javascript - Angular 4 的 karma 测试期间出现错误 'Missing: SyncTestZoneSpec'

标签 javascript angular typescript karma-jasmine

首先:我的项目在分支 /develop 上完全正常,测试通过。

我创建了一个分支来清理 imports 并在每次必须访问类时使用别名而不是 ../../../../。我将它添加到 tsconfig.json 中:

"baseUrl": "src",
    "paths": {
      "@app/*": [
        "app/*"
      ],
      "@core/*": [
        "app/core/*"
      ],
      "@common/*": [
        "app/common/*"
      ],
      "@models/*": [
        "app/models/*"
      ],
      "@env/*": [
        "environments/*"
      ],
      "@assets/*": [
        "assets/*"
      ]
    }

我刚刚完成,但是当使用简单的 npm run test 执行测试时,它会做类似这样的事情是认为 karma start ./karma.conf.js --log-level error 我得到这个错误:

HeadlessChrome 67.0.3396 (Windows 10.0.0) ERROR
  Uncaught Error: Missing: SyncTestZoneSpec
  at http://localhost:9876/_karma_webpack_/vendor.bundle.js:270128

我所更改的只是我上面所说的,这个错误告诉我什么?


编辑:使用 github 链接更正

通过将 zone.js 版本更新为 0.8.26 并仅用一行替换 test.ts 中的导入来更正此问题:

导入 'zone.js/dist/zone-testing';

但是现在我在所有测试中都遇到了这个错误:

HeadlessChrome 67.0.3396 (Windows 10.0.0) SomeService #getCurrentUser should return user object FAILED
        TypeError: Cannot read property 'assertPresent' of undefined
            at resetFakeAsyncZone node_modules/@angular/core/@angular/core/testing.es5.js:308:1)
            at Object.<anonymous> node_modules/@angular/core/@angular/core/testing.es5.js:1015:1)
            at ZoneQueueRunner.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.jasmine.QueueRunner.ZoneQueueRunner.execute node_modules/zone.js/dist/zone-testing.js:437:1)
HeadlessChrome 67.0.3396 (Windows 10.0.0): Executed 120 of 120 (120 FAILED) ERROR (4.725 secs / 4.633 secs)

Relatedd issue on github但暂时没有解决方案。

我的 test.ts 的内容:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

import 'zone.js/dist/zone-testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare const __karma__: any;
declare const require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

最佳答案

尝试将 zone-testing 导入移动到像这样的第一个导入中:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from 
'@angular/platform-browser-dynamic/testing';

摘自此错误报告:All tests failed if order of imports is changed in test.ts

关于javascript - Angular 4 的 karma 测试期间出现错误 'Missing: SyncTestZoneSpec',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50950740/

相关文章:

javascript - 在 loopback 4 中创建 requestBody 的最佳实践是什么?

Angular:使用搜索输入和特定过滤器选项过滤表格内容

javascript - 从 javascript 调用 ItemEditForm 上的保存操作

javascript - leaflet.js - 单击时设置标记,拖动时更新位置

angular - 无法按照教程使用 angular-in-memory-web-api 0.6.1

javascript - Angular 垫输入

typescript - 使用 Typescript 为 Vue 项目设置单元测试失败,出现 TypeError : Cannot destructure property `polyfills` of 'undefined' or 'null'

javascript - Bootstrap-多选显示隐藏div元素

javascript - 如何将 JavaScript 对象转换为 YUI 3 节点?

javascript - 在 Angular 2 中显示从子组件到父组件的已检查 id