gerhard
gerhard

Reputation: 447

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.

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.

What could be the issue here?

Upvotes: 2

Views: 2339

Answers (1)

gerhard
gerhard

Reputation: 447

So the problem was I needed to add import 'zone.js/dist/fake-async-test'; to the testing entry file.

Upvotes: 5

Related Questions