简体   繁体   English

在测试中使用 fakeAsync 会导致 Angular 4 和 Zone.js 出错

[英]Using fakeAsync in tests results in error with Angular 4 and Zone.js

I'm using Angular 4.3.1 and zone.js 0.8.14 and I'm running my tests, with karma and webpack and came across this weird error in my tests whenever I'm using fakeAsync and tick for time-related tests.我正在使用 Angular 4.3.1zone.js 0.8.14并且我正在使用karmawebpack运行我的测试,并且每当我使用fakeAsync并勾选与时间相关的测试时,都会在我的测试中遇到这个奇怪的错误。

The test case:测试用例:

it('should call the function which is given as parameter', fakeAsync(() => {
  autoRefreshService.start();
  expect(spyFn).not.toHaveBeenCalled();
  tick(1);
  expect(spyFn).toHaveBeenCalled();
}));

The test invoking file:测试调用文件:

require('core-js/es6');
require('core-js/es7/reflect');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy');
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
import { TestBed } from '@angular/core/testing';
import {
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
const context = (require as any).context('./', true, /\.spec\.ts$/);
context.keys().map(context);

And the error I'm getting is:我得到的错误是:

Chrome 59.0.3071 (Mac OS X 10.12.5) CallbackRepeaterService start should not start if no callback set FAILED
    TypeError: Right-hand side of 'instanceof' is not an object
        at Object.<anonymous> (src/test.ts:25139:49)
        at ZoneDelegate.invoke (src/test.ts:86299:26)
        at ProxyZoneSpec.onInvoke (src/test.ts:88697:39)
        at ZoneDelegate.invoke (src/test.ts:86298:32)
        at Zone.run (src/test.ts:86049:43)
        at Object.<anonymous> (src/test.ts:88992:34)
        at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (src/test.ts:89020:42)

The error message must be related to fakeAsync , because if I remove it, it doesn't complain.错误消息必须与fakeAsync相关,因为如果我删除它,它不会抱怨。

What could be the issue here?这里可能是什么问题?

So the problem was I needed to add import 'zone.js/dist/fake-async-test';所以问题是我需要添加import 'zone.js/dist/fake-async-test'; to the testing entry file.到测试入口文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM