簡體   English   中英

升級到Angular 6 w / CLI后,測試將無法運行

[英]Tests won't run after upgrading to Angular 6 w/ CLI

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

這里有一些將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個..但是在右側的源中,您可以清楚地看到測試。

在此輸入圖像描述

我有另一個使用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();

所以正則表達式是錯誤的

\\.fake-test\\.ts$

fake-test\\.ts$

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

我希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM