【问题标题】:Tests won't run after upgrading to Angular 6 w/ CLI升级到带有 CLI 的 Angular 6 后测试不会运行
【发布时间】:2018-10-25 08:29:05
【问题描述】:

使用 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);

fake-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 个。但在右侧的源代码中,您可以清楚地看到一个测试。

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

【问题讨论】:

  • 你可以尝试将业力升级到~2.0.0吗?
  • 在你的 test.js 中是 context.keys().map(context); 最后一行?
  • 是的,这是最后一行.. 尝试 karma 2.0.0 结果相同,我将更新问题中的版本

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


【解决方案1】:

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

\.fake-test\.ts$

fake-test\.ts$

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

希望对你有所帮助。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这不是必需的,我有一个没有 karma.start() 的新项目可以工作.. angular cli 在引擎盖下启动它.. 找不到规范出于某种原因,即使我在源文件中看到它们(chrome 调试器)
    • 听说jasmine-core和karma-jasmine存在一些兼容性问题。试试这些版本,看看“jasmine”:“2.5.2”、“jasmine-core”:“2.5.2”、“jasmine-spec-reporter”:“4.2.1”、“karma-jasmine”:“1.1。 2",
    • 感谢@NadhirFalta 提供的版本信息。我还需要:“karma-jasmine-html-reporter”:“0.2.2”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2019-12-02
    相关资源
    最近更新 更多