gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:32:54 27 4
gpt4 key购买 nike

使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com