Skip to content

Instantly share code, notes, and snippets.



Last active Dec 8, 2018
What would you like to do?
One Angular Test can explain asynchronicity in Angular
it('setTimeout & tick & flushMicrotasks', fakeAsync(() => {
let state = [];
// add to the micro task queue — will be the first thing executed after this script block is done
Promise.resolve().then(function() {
state.push('promise result');
// add to the task queue — will be executed after this task and when the micro task queue is emptied
setTimeout(() => { state.push('timeout called'); });
// add to the task queue after 2 seconds
setTimeout(() => { state.push('timeout called after 2s'); }, 2000);
expect(state).toEqual(['promise result']);
expect(state).toEqual(['promise result', 'timeout called']);
expect(state).toEqual(['promise result', 'timeout called', 'timeout called after 2s']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.