2

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

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

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

我刚刚完成,但是当使用 simple 执行测试时,执行npm run test类似 thisis 的操作时,我认为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 中的导入替换为一行来纠正此问题:

import '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)

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();
4

1 回答 1

0

尝试将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';

取自此错误报告:如果在 test.ts 中更改导入顺序,则所有测试都失败

于 2018-06-28T13:42:45.307 回答