FakeAsync's tick flushs nested timeouts
See original GitHub issue🐞 bug report
Affected Package
The issue is caused by package @angular/core/testing
Is this a regression?
dunno
Description
tick
call flushs even nested timeouts that have not been installed at the call moment
🔬 Minimal Reproduction
function waitTwice(callback: () => any): void {
window.setTimeout(() =>
window.setTimeout(() => callback())
);
}
describe('fakeAsync', () => {
it('nested timers', fakeAsync(() => {
const callback = jasmine.createSpy('callback');
waitTwice(callback);
expect(callback).not.toHaveBeenCalled();
tick();
expect(callback).not.toHaveBeenCalled();
}));
});
repo with this test
git clone git@github.com:artaommahe/fakeasync-nested-timeout.git
yarn install
yarn test
🔥 Exception or Error
Chrome 78.0.3904 (Windows 10.0.0) fakeAsync nested timers FAILED
Error: Expected spy callback not to have been called.
🌍 Your Environment
Angular Version:
Angular CLI: 8.3.18
Node: 10.16.1
OS: win32 x64
Angular: 8.2.13
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.18
@angular-devkit/build-angular 0.803.18
@angular-devkit/build-optimizer 0.803.18
@angular-devkit/build-webpack 0.803.18
@angular-devkit/core 8.3.18
@angular-devkit/schematics 8.3.18
@angular/cli 8.3.18
@ngtools/webpack 8.3.18
@schematics/angular 8.3.18
@schematics/update 0.803.18
rxjs 6.4.0
typescript 3.5.3
webpack 4.39.2
Anything else relevant?
Same test works well with jest’s timer functions
jest.useFakeTimers();
const callback = jest.fn();
waitTwice(callback);
jest.runOnlyPendingTimers();
expect(callback).not.toHaveBeenCalled();
jest.runOnlyPendingTimers();
expect(callback).toHaveBeenCalled();
It is a real world case (function with nested zero-timeouts) - we are forced to use this due to parent-child init hooks order for custom elements and for manual elements creation (creating components tree from string markup) when parent’s ngAfterViewInit is called sooner than child’s one.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
timer still in the queue despite flush in fakeAsync
I was still getting this error in my test for nested timeout, a timeout inside a service that is inside a request subscribe....
Read more >Testing timers with fakeAsync - Damir's Corner
Does your code use a polling timeout? The code is constantly creating new timers, so the flush method can never clear them all....
Read more >Component testing scenarios - Angular
To use fakeAsync() functionality, you must import zone.js/testing in your test ... toHaveBeenCalled(); tick(0); // the nested timeout will also be triggered ...
Read more >[Unit Testing Angular] fakeAsync - Zhentiw - 博客园
import { fakeAsync, flush, flushMicrotasks, tick } from ... forworad 1000ms // tick(1000); // exec all the timeouts flush(); expect(test).
Read more >Angular Test tick() - ConcretePage.com
Then for tick function call, the nested setTimeout function will not execute. it ('test with nested setTimeout', fakeAsync(() => ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It is a bug, Thanks for posting the issue. I will fix this one asap.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.