Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

A few months ago one of the developers I work with ran into the below error in an Angular unit test:

1 timer(s) still in the queue

The fix was to specify a particular amount of time in the tick() call as shown below:

it('should set accountConfirmationDetails when getAccountConfirmationDetails is called', fakeAsync(() => {
        // Arrange
        let spyGetAccountConfirmationDetails = spyOn(accountService, 'getAccountConfirmationDetails')
        .and
        .returnValue(Promise.resolve(AccountMockData.mockAccountConfirmationDetails));

       
        // Act
        fixture.detectChanges();
       tick(15000);

        // Assert
        expect(spyGetPaymentConfirmationDetails).toHaveBeenCalled();
        expect(comp.zone).toBe(42);
        expect(comp.hasValidProfile).toBeTruthy();
}));

Hoping this helps someone!

Comments

Juan Mendes said…
Helped me! I also wanted to point out that the `tick()` has to be called in the same `fakeAsync` zone. I tried doing it from `afterEach(fakeAsync())` to avoid copy pasting it and it did not work!
Cormick said…
Alan this helped me. Thank you for posting it.

I still don't understand what fixture.detectChanges() is doing that causes the timer to be stuck in the queue. I also don't know how to see tasks that are in the queue. Do you?
kossmoboleat said…
Thanks that was useful! I had found the same problem somewhere else, but googling it and finding your note was actually easier than remembering that ;-)

Did you ever find out why this is the case? I suspect it's because of some asynchronous operation that's started but *not recognized* by tick() but *is* recognized by the angular zone when it realizes there are still timers in the queue...
Anonymous said…
I am specifying an amount of time (500) and it still happens anyway. Does it have to be 15000? That seems like a long time.
Anonymous said…
heled me
thanks
Anonymous said…
GREAT IT SAVED MY LIFE.

I was about to Talon E from balcony.

:D

Now i can move on to test some more.

So you delayed the Talon E.

Thank you

Popular Posts

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

How to Determine if a Column Exists in a DataReader

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Setting Default Values in an Angular Reactive Form