问题
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.
回答1:
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!
回答2:
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
来源:https://stackoverflow.com/questions/50983015/testing-cannot-read-property-assertpresent-of-undefined-at-resetfakeasynczon