I have this test that will result in the infamous "1 timer(s) still in the queue" error:
import {
discardPeriodicTasks,
fakeAsync,
flush,
flushMicrotasks,
tick
} from "@angular/core/testing";
describe("Sleep", () => {
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
it("should sleep async", async () => {
let slept = false;
await sleep(0).then(() => (slept = true));
expect(slept).toBeTruthy();
});
it("should sleep fakeAsync", fakeAsync(async () => {
let slept = false;
await sleep(0).then(() => (slept = true));
flush();
flushMicrotasks();
discardPeriodicTasks();
tick(1000);
expect(slept).toBeTruthy();
}));
});
No amount of flushing or ticking including the hints from this answer will get rid of the timer. What else can I do? The variant without fakeAsync()
works fine.
Stackblitz: https://stackblitz.com/edit/test-jasmine-karma-fakeasync-timer?file=app/test.ts