Skip to content

FakeAsync's tick flushs nested timeouts #33799

@artaommahe

Description

@artaommahe

🐞 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 [email protected]:artaommahe/fakeasync-nested-timeout.git
  • yarn install
  • yarn test

test file

🔥 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions