Skip to content

Instantly share code, notes, and snippets.

@alaindet
Created June 20, 2022 08:56
Show Gist options
  • Select an option

  • Save alaindet/97f86dc4c228166130ffdfeb7b50f94e to your computer and use it in GitHub Desktop.

Select an option

Save alaindet/97f86dc4c228166130ffdfeb7b50f94e to your computer and use it in GitHub Desktop.
Async testing in Angular
it('With promises and setTimeouts', fakeAsync(() => {
let counter = 0;
Promise.resolve().then(() => {
counter += 10;
setTimeout(() => counter += 1, 100);
});
expect(counter).toBe(0);
flushMicrotasks(); // Run promises
expect(counter).toBe(10);
tick(50); // Simulate 50 milliseconds have passed
expect(counter).toBe(10);
tick(50); // Simulate 50 milliseconds have passed
expect(counter).toBe(11);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment