Skip to content

Instantly share code, notes, and snippets.

@georgiee georgiee/spec.ts
Last active Dec 8, 2018

Embed
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([]);
flushMicrotasks();
expect(state).toEqual(['promise result']);
tick();
expect(state).toEqual(['promise result', 'timeout called']);
tick(2000);
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.