Created
June 20, 2022 08:56
-
-
Save alaindet/97f86dc4c228166130ffdfeb7b50f94e to your computer and use it in GitHub Desktop.
Async testing in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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