3

使用 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",

下面是更多将业力日志记录设置为 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 个。但是在右侧的源代码中,您可以清楚地看到一个测试。

在此处输入图像描述

我有另一个使用 CLI 的新 Angular 6 项目,它正在运行。我无法确定任何重大差异。

4

2 回答 2

0

所以正则表达式是错误的,而是

\.fake-test\.ts$

fake-test\.ts$

基本上你在等一个“。” 在“假”之前.fake-test.ts

我希望它会有所帮助。

于 2018-06-14T13:00:13.000 回答
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();
于 2018-05-15T14:05:05.220 回答