我曾经使用ng new my-app --minimal
过Angular 5 CLI应用程序。经过一会儿之后,我发现我确实想要一个spec文件。所以我做的是:
/src/app/my-thing.ts
用类创建文件/src/app/my-thing.spec.ts
包含内容的文件describe('MyThing', () => { ... }
但是显然describe(...)
找不到,因为--minimal
跳过设置测试,因此没有任何支持。
如何正确添加可以正常使用的测试设置ng test
?
我更想了解是否有基于CLI的解决方案,但是目前,我只是继续使用并使用了git,diff和cli magic来查看启动和运行所需的内容。这是愚蠢的解决方案:
更新.angular-cli.json
删除:
"class": {
"spec": false
}
.angular-cli.json
将设置更新"spec": false
到true
内部的任何地方"component"
将它们添加到package.json
:
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
并运行npm install
以安装这些软件包。
将此(或类似)添加karma.conf.js
到项目根目录下的新文件中:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
将此添加到新文件src/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, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
添加此新文件src/tsconfig.spec.json
:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": "./",
"module": "commonjs",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
现在,您应该可以运行ng test
并查看测试运行了。
希望这对某人有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句