javascript - 使用角度 4 进行业力测试期间出现错误“缺少:SyncTestZoneSpec”
问题描述
首先第一件事:我的项目在分支上完全正常/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();
解决方案
尝试将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 中更改导入顺序,则所有测试都失败
推荐阅读
- shell - 在我的鹦鹉安装脚本编辑中找不到匹配 if 的问题
- dart - 如何在选择时更改列表拼贴的布局?
- angular - 在多个字段上搜索管道以查找一组项目
- bash - 在 Ubuntu 18.04 上使用 bash 编写“sudo apt upgrade”脚本时,如何自动绕过“包配置”屏幕?
- byte-buddy - 如何使用 Byte Buddy 从类的每个字段中获取值?
- javascript - 有没有更好的方法从构造函数返回对象和构造函数?
- reactjs - 当组件订阅列表时如何反应重新渲染组件
- gps - 使用 GNSS SDR sim 演示欺骗
- swift - Core Data 抽象实体超级初始化器
- javascript - 试图从后端发送一个 zip 到前端