我正试图通过ng test
为我的Angular 2 App运行单元测试,但我一直收到以下错误:
ERROR in Entry module not found: Error: Can't resolve
'/Users/username/Dev/dashboard/src/test.ts' in '/Users/username/Dev/dashboard/node_modules/@angular/cli/models/webpack-configs'
28 07 2017 15:18:35.337:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
28 07 2017 15:18:35.441:INFO [launcher]: Trying to start Chrome again (1/2).
28 07 2017 15:19:35.447:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
28 07 2017 15:19:35.587:INFO [launcher]: Trying to start Chrome again (2/2).
Angular CLI版本(在npm更新之后):@angular/cli: 1.2.6
节点版本为:node: 8.0.0
其他命令:ng build -prod
和:ng serve
正常运行且没有错误,我可以在浏览器上访问该应用。
我必须提到的一件事是,几天前,我确实从文件夹中删除了所有.spec.ts
文件作为所有方法:
describe(), beforeEach(), it()
无法找到。搜索后,我发现应用程序在删除时不会中断,但今天我再次创建它们以运行应用程序的单元测试。
请问好吗?
答案 0 :(得分:0)
我也遇到了同样的问题,让我觉得您的test.ts
文件丢失了,这是它的内容:
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare const __karma__: any;
declare const require: any;
// Prevent Karma from running prematurely.
// tslint:disable-next-line:only-arrow-functions
__karma__.loaded = function () {};
// 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);
// Finally, start Karma to run the tests.
__karma__.start();
只需将其保存在app/src/test.ts
中,然后重新运行ng test
。