angular - 无法在 Angular + Karma 中正确测试不属于项目目录的文件
问题描述
我有以下文件结构:
projects
myproj
- 角应用myproj-lib
- 角度库
shared
- 共享代码,在应用程序和库中使用
两个项目(App 和 Lib)都是使用 Angular CLI 构建的(angular.json 到目前为止没有改变,除了codeCoverage
选项)。我无法运行属于shared
目录的测试:
- 对于应用程序,测试运行正常,但覆盖范围仅包括项目文件(下的
projects/myproj/src
) - 对于库测试,
Error: \TRUNCATED_PATH\shared\components\component-dir\component.ts
TypeScript 编译中缺少它总是会中断
我认为应用程序的问题是覆盖率报告者没有看到shared
目录,尽管测试(甚至最终报告)包括这些(它只显示src
在项目中)
至于图书馆,我想这与 Karma 如何解析路径有关,以便 Typescript 可以看到它们。
我的test.ts
文件(仅添加了其他路径):
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone-testing-bundle';
import { getTestBed } from '@angular/core/testing'; // tslint:disable-line
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const projectContext = require.context('./', true);
const sharedContext = require.context('../../../shared', true);
// And load the modules.
sharedContext.keys().map(sharedContext);
projectContext.keys().map(projectContext);
我的karma.conf.js
:
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '../../shared',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-mocha-reporter'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../coverage'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['mocha'],
mochaReporter: {
ignoreSkipped: true
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'],
singleRun: false,
});
};
我的tsconfig.spec.json
(我试图修改路径,但没有帮助):
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "../../",
"rootDir": "../../",
"outDir": "../../out-tsc/spec",
"types": [
"jasmine",
"node"
],
"module": "esnext",
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"importHelpers": true,
"preserveSymlinks": true
},
"include": [
"./src/**/*.ts",
"./src/**/*.spec.ts",
"../../shared/**/*.ts",
"../../shared/**/*.spec.ts"
],
"exclude": []
}
有任何想法吗?
解决方案
问题归结为shared
在编译中包含两次文件。如下就足够了projectContext.keys()
:test.ts
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone-testing-bundle';
import { getTestBed } from '@angular/core/testing'; // tslint:disable-line
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const projectContext = require.context('./', true);
// And load the modules.
projectContext.keys().map(projectContext);
这是有效的,因为shared
在lib
目录中是符号链接的。由于 Git 的实现,Symlink 甚至可以在 Unix/Windows 之间移植。那解决了!
所以总结一下:
- 将符号链接添加到
shared
项目内的目录而不是使用真实目录 - 删除重复的
require.context
- 使用符号链接目录中的文件,而不是在项目之外使用两条路径
推荐阅读
- reactjs - 为什么在我输入 TextField ReactJS 时会触发按钮的 onClick 事件
- c - 面临寻找大数立方体的问题
- apache-kafka - 如何为 Kafka 中的集群配置 server.properties
- java - 如何从集合中的excel数据中找到一些字符串元素?.contains 方法在我的情况下不起作用
- javascript - 如何使用不同的表 id 显示两个 html 表?
- python - PyInstaller Orange3 __main__ Python 脚本调用不同文件夹中的脚本不起作用
- java - 保存后休眠插入
- javascript - 页面显示纯 html 文本而不是呈现 html 表
- java - Java对工厂方法的DAO定义
- dask - 在所有 dask.distributed worker / scheduler 中维护一个 git 存储库