Testing | Cannot read property 'assertPresent' of undefined at resetFakeAsyncZone
Asked Answered
U

5

11

I have a problem with karma v1.4. testing framework. All my unit tests are now failing with error Cannot read property 'assertPresent' of undefined at resetFakeAsyncZone

I've already searched for solutions and tested them, but unfortunately none helped. The solutions suggests that I should change the order of imports in my test.js file. I've done that.

This is the suggested order I am using, but it still fails:

import 'zone.js/dist/zone.js'; // 1st
import 'zone.js/dist/async-test'; // 2nd
import 'zone.js/dist/fake-async-test'; // 3rd
import 'zone.js/dist/long-stack-trace-zone'; // 4th
import 'zone.js/dist/sync-test'; // 5th
import 'zone.js/dist/proxy.js'; // 6th
import 'zone.js/dist/jasmine-patch'; // 7th

PS: I am using VS Code, which now automatically sorts imports upon file save and thus changes my custom order of imports, which is super annoying in this case. I do not know how to disable it for specific file only, so I have to edit my test.js file in Notepad.

Unrestrained answered 22/6, 2018 at 7:40 Comment(0)
V
32

Which version of zone.js is used?

In the newer version of zone.js, there is no need to load each test lib separately.

Place the following at the top of test.ts.

import 'zone.js/dist/zone-testing';

Note: It is important that this import is before any other imports!

Vitellus answered 22/6, 2018 at 9:27 Comment(6)
I am using version 0.8.26, but that import did not change anything. I am still getting the same errorUnrestrained
Could you provide a reproduce repo?Vitellus
Had the same problem with the same zone version. Solution was not to reorder imports in test.ts: Put import 'zone.js/dist/zone-testing'; before every other import.Unhinge
@Unhinge Thanks for pointing this out. Implementing tslint --fix with alphabatized imports rule will introduce this issue for the exact reason you point out!Statecraft
"at the top" is really importantMarylandmarylee
To be absolutely clear, it should be before any other imports, except your existing polyfills, which is where most of us put the core zone.js/dist/zone. zone-testing needs to have zone loaded first.Lea
H
3

I experienced this problem after VS Code re-ordered my imports.

The fix is to include the 2 zone imports first - I'm adding a new answer b/c zone-testing needs to come after zone. (If you put zone-testing before zone, you'll get a Zone is not defined in ..../vendor.js error).

Here is the full code for a working test.ts file:

import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing'; // AFTER zone, BEFORE everything else

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';


declare const require: {
  context(path: string, deep?: boolean, filter?: RegExp): {
    keys(): string[];
    <T>(id: string): T;
  };
};

// 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);
Hammack answered 14/12, 2020 at 23:39 Comment(0)
W
1

here is the order and you can add this:

// tslint:disable-next-line:ordered-imports

to disable import order for the next line

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
// tslint:disable-next-line:ordered-imports
import 'zone.js/dist/jasmine-patch';
// tslint:disable-next-line:ordered-imports
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';

or disable it for the block

// tslint:disable:ordered-imports
 import 'zone.js/dist/long-stack-trace-zone';
 import 'zone.js/dist/proxy.js';
 import 'zone.js/dist/sync-test';
 import 'zone.js/dist/jasmine-patch';
 import 'zone.js/dist/async-test';
 import 'zone.js/dist/fake-async-test';

and run your test like this to see the exact error:

ng test -sm=false
Welfarism answered 22/6, 2018 at 8:14 Comment(2)
Thank you, but it still fails with the same error. I have upgraded the npm packages related to unit testing to their latest versions, but it did not help.Unrestrained
run your test like this to see the exact error : ng test -sm=falseWelfarism
L
1

Best Solution is to use jasmine clock

it('test', () => {
  // arrange
  const clock = jasmine.clock();
  clock.install();
  ... other arrangements

  // act
  ... actions
  clock.tick(2000);

  // assert
  clock.uninstall();
  ... asserts
});
Lorica answered 2/9, 2020 at 17:27 Comment(0)
E
0

This error occurred when a Jasmine test was using fakeAsync and tick. Removing fakeAsync/tick resolved this issue.

Exterior answered 6/2, 2020 at 21:8 Comment(4)
Why was it there in the first place?Hildagarde
This worked for me, not sure why you get all these downsJugular
what if I need the fakeasync + tick?Kukri
setTimeout in the code being tested can cause this issue. Try using flush() or tick(<millis>).Exterior

© 2022 - 2024 — McMap. All rights reserved.