This test passes but results in a problem:
it('should retrieve data on getDownloadProgress() call', (done: DoneFn) => { let response = { 'process': {}, 'success': 'success', } as IPollResponse; httpClientSpy.get.and.returnValue(of(response)); service.getDownloadProgress(1)!.subscribe((result: any) => { expect(result).toEqual(response); done(); }); });
There is an error that displays when running tests in Jasmine and it falsely indicates test failure:
An error was thrown in afterAll Error: An asynchronous spec, beforeEach, or afterEach function called its 'done' callback more than once.
The bug that causes this error is fixed in the latest version of zone.js
according to this thread:
https://github.com/angular/angular/issues/45476
@angular/core
depends on zone.js
version 0.11.4
in its peerDependencies
which is revealed in package-lock.json
:
"node_modules/@angular/core": { "version": "14.2.7", "license": "MIT", "dependencies": { "tslib": "^2.3.0" }, "engines": { "node": "^14.15.0 || >=16.10.0" }, "peerDependencies": { "rxjs": "^6.5.3 || ^7.4.0", "zone.js": "~0.11.4" } },
I believe that if I can force @angular/core
to use the latest version of zone.js
this error will stop appearing.
How can I force @angular/core
to use a specific version of zone.js
?
Advertisement
Answer
I don’t know if forcing @angular/core
to use a specific version of zone.js
is a good idea because a different version may fix this problem but it can create other problems. There could be a good reason that @angular/core
wants that version of zone.js
.
To fix your issue, usually in Angular unit tests, I make sure I unsubscribe from the subscriptions especially if they are not one and done.
Try something like this to fix your issue:
service.getDownloadProgress(1)!.pipe(take(1)).subscribe((result: any) => { expect(result).toEqual(response); done(); });
The take(1)
operator will only take one emission and call the callback and therefore done()
should only be called once.