angular - 升级到带有 CLI 的 Angular 6 后,测试将不会运行

标签 angular jasmine angular-cli karma-jasmine angular-cli-v6

使用 Angular CLI 将带有 webpack 的 Angular 5 项目升级到 Angular 6。 测试现在不会运行并出现以下错误。

Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
Chrome 66.0.3359 (Mac OS X 10.13.4) ERROR
   An error was thrown in afterAll
Chrome 66.0.3359 (Mac OS X 10.13.4) ERROR
  An error was thrown in afterAll
  Uncaught TypeError: env.catchExceptions is not a function
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 ERROR (0 secs / 0 secs)
Chrome 66.0.3359 (Mac OS X 10.13.4) ERROR
  An error was thrown in afterAll
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 ERROR (0.006 secs / 0 secs)

我在 test.ts 中更改了我的上下文,这样它就不会运行我的所有测试,而是只运行我设置的单个测试,但它仍然失败并出现相同的错误。

测试.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/zone-testing';
import 'rxjs-compat';
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$/);
const context = require.context('./', true, /\.fake-test\.ts$/);
// And load the modules.
context.keys().map(context);

假测试.ts

describe('fakeTest', () => {
    it('fakeAssert', () => {
        expect(true).toBe(true);
    });
});

这里是package.json中的相关版本

"@angular/cli": "6.0.0",
"@angular/compiler-cli": "6.0.0",
"@angular-devkit/build-angular": "~0.6.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",

这里有一些更多的日志记录,karma 日志记录设置为 LOG_DEBUG:

15 05 2018 08:39:25.913:DEBUG [middleware:source-files]: Requesting /_karma_webpack_/scripts.js /
15 05 2018 08:39:25.913:DEBUG [middleware:source-files]: Fetching /_karma_webpack_/scripts.js
15 05 2018 08:39:25.914:DEBUG [middleware:source-files]: Requesting /_karma_webpack_/vendor.js /
15 05 2018 08:39:25.914:DEBUG [middleware:source-files]: Fetching /_karma_webpack_/vendor.js
15 05 2018 08:39:25.914:DEBUG [middleware:source-files]: Requesting /_karma_webpack_/main.js /
15 05 2018 08:39:25.914:DEBUG [middleware:source-files]: Fetching /_karma_webpack_/main.js
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
15 05 2018 08:39:26.139:DEBUG [karma]: Run complete, exiting.
15 05 2018 08:39:26.140:DEBUG [launcher]: Disconnecting all browsers

这是我在 Chrome 中的测试运行程序的图片,显示了 0 个测试中的 0 个。但在右侧的源代码中,您可以清楚地看到一个测试。

enter image description here

我有另一个使用 CLI 的新 Angular 6 项目,它正在运行。我无法识别出任何显着差异。

最佳答案

您在 test.ts 中遗漏了一行实际启动 Karma 的代码:

// Finally, start Karma to run the tests.
__karma__.start();

你的 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 let __karma__: any;
declare let require: any;

// Prevent Karma from running prematurely.
// tslint:disable-next-line
__karma__.loaded = function () {
  //leave this empty
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context: any = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

关于angular - 升级到带有 CLI 的 Angular 6 后,测试将不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50351321/

相关文章:

angular - Dockerizing Angular 2应用程序失败

从 node_modules Angular 延迟加载

angular - 使 Electron 应用程序允许从本地文件系统下载pdf文件

javascript - 在Sinon 中,spyOn.and.callFake 相当于什么?

angular - 禁用按钮的单元测试

Angular 9 - Istanbul 尔测试覆盖率报告生成为空

angular - ng build --prod 非常慢

angular - 在 *ngFor 中使用带可变参数的 Slice Pipe

angular - 如何防止 PWA 在新的 Safari 窗口中打开链接和 Auth0 单点登录身份验证

jasmine - 属性 'Ajax' 在类型 'typeof jasmine' 上不存在