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

时间:2017-07-22 13:19:47

标签: angular webpack jasmine zone.js

我正在使用Angular 4.3.1zone.js 0.8.14而我正在使用karmawebpack运行我的测试,并遇到了这个奇怪的错误在我的测试中,每当我使用fakeAsync并打勾进行与时间相关的测试时。

测试用例:

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

测试调用文件:

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

我得到的错误是:

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)

错误消息必须与fakeAsync相关,因为如果我将其删除,则不会抱怨。

这可能是什么问题?

1 个答案:

答案 0 :(得分:0)

所以问题是我需要将import 'zone.js/dist/fake-async-test';添加到测试条目文件中。