Components: Unit tests using OverlayRef fails with Error: 1 timer(s) still in the queue.

Created on 28 Sep 2018  路  4Comments  路  Source: angular/components

All my component unit tests that are exercising Material components that make use of OverlayRef, like mat-select throw an error when my unit test destroys the test fixture due to a setTimeout(500) in OverlayRef.detachBackdrop:

https://github.com/angular/material2/blob/5321ab3cc4104369ea756857210c724f6c028cea/src/cdk/overlay/overlay-ref.ts#L436

It would be really awesome if OverlayRef.ngOnDestory this timeout would be cleared with clearTimeout(timeoutId).

Currently the only workarounds is to call tick(500) at the end of my unit tests. I don't like this because it artificially delays all my unit tests.

Most helpful comment

Explicitly calling fixture.destroy() at the end of unit tests causes the overlay-ref to close the backdrop, but it schedules a new task in zonejs: https://github.com/angular/material2/blob/5321ab3cc4104369ea756857210c724f6c028cea/src/cdk/overlay/overlay-ref.ts#L436

To workaround this issue I have been able to do the following at the end of each test:

  fixture.destroy();
  flush();

By calling flush() the Scheduler of the FakeAsyncTestZoneSpec has it's non-periodic tasks flushed, leaving no tasks in the scheduler's queue.

All 4 comments

It does get cleared (see https://github.com/angular/material2/blob/5321ab3cc4104369ea756857210c724f6c028cea/src/cdk/overlay/overlay-ref.ts#L416), however that callback depends on a transitionend event being fired on the backdrop which won't happen on its own during a unit test.

Closing since this behavior is unlikely to change.

Explicitly calling fixture.destroy() at the end of unit tests causes the overlay-ref to close the backdrop, but it schedules a new task in zonejs: https://github.com/angular/material2/blob/5321ab3cc4104369ea756857210c724f6c028cea/src/cdk/overlay/overlay-ref.ts#L436

To workaround this issue I have been able to do the following at the end of each test:

  fixture.destroy();
  flush();

By calling flush() the Scheduler of the FakeAsyncTestZoneSpec has it's non-periodic tasks flushed, leaving no tasks in the scheduler's queue.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dzrust picture dzrust  路  3Comments

michaelb-01 picture michaelb-01  路  3Comments

jelbourn picture jelbourn  路  3Comments

Hiblton picture Hiblton  路  3Comments

vitaly-t picture vitaly-t  路  3Comments