升级到Angular 6 w / CLI后,测试将无法运行

时间:2018-05-15 13:19:54

标签: 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中更改了我的上下文,这样我就可以运行我设置的单个测试而不是运行所有测试,但仍然会出现相同的错误。

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';
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);

假test.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",

这里有一些日志记录设置为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项目,它正在运行。我一直无法发现任何重大差异。

2 个答案:

答案 0 :(得分:0)

你的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();

答案 1 :(得分:0)

所以正则表达式错了,而不是

\.fake-test\.ts$

fake-test\.ts$

基本上你在等待"。"之前"假" .fake-test.ts

我希望它会有所帮助。