我将所有测试文件设置为与src
文件所在的目录不同,测试将具有与src
相同的文件结构,但唯一的区别是它们位于要测试的基本文件夹中。我认为只需更改angular.json
测试设置并将配置测试文件移动到其他目录即可,但是似乎没有测试文件被业力测试。
为了检查一切是否正常,我只进行了一次测试
#src
src/webapp/app/pipes/
- time.ts
#test
test/webapp/
- karma.conf.js
- tsconfig.spec.json
- test.ts
test/webapp/pipes/
- time.ts
angular.json
中的测试部分设置如下:
{
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "test/webapp/test.ts",
"polyfills": "src/webapp/polyfills.ts",
"tsConfig": "test/webapp/tsconfig.spec.json",
"karmaConfig": "test/webapp/karma.conf.js",
"styles": [
"src/webapp/styles.css"
],
"scripts": [],
"assets": [
"src/webapp/favicon.ico",
"src/webapp/assets"
]
}
}
karma.conf.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
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'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
tsconfig.spec.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts",
"../../src/webapp/polyfills.ts"
],
"include": [
"**/*.ts",
"**/*.d.ts"
]
}
test.ts
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
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 context = require.context('./', true /\.ts$/);
// And load the modules.
context.keys().map(context);
我认为一切设置都很好,但是我不知道为什么它不能检测到测试目录中的文件,控制台不显示警告或可能显示此问题原因的内容,并且运行程序始终显示Executed 0 of 0 ERROR (0.012 secs / 0 secs)
我发现的其他问题都没有帮助。.我真的不想遵循默认的规范结构,该结构将测试放在src文件所在的目录中。以防万一,必要时,我还将放置测试文件的内容
test / webapp / app / pipes / time.ts
import { TimePipe } from "../../../../src/webapp/app/pipes/time";
describe("Time pipe test", () => {
let Time;
beforeEach(() => {
Time = new TimePipe();
});
it("should correctly show minutes and seconds in a time value", () => {
const data = 74;
const expected = "01:14";
expect(Time.transform(data)).toBe(expected);
});
});