Skip to content

Instantly share code, notes, and snippets.

@jonrimmer
Created May 3, 2018 09:32
Show Gist options
  • Save jonrimmer/6d7fcf4c1bcbaed126e57a8e457016aa to your computer and use it in GitHub Desktop.
Save jonrimmer/6d7fcf4c1bcbaed126e57a8e457016aa to your computer and use it in GitHub Desktop.
Clearing all pending tasks from the fake async zone
import { fakeAsync, tick } from '@angular/core/testing';
import { discardFakeAsyncTimers ) from './helpers';
const myComponent = {};
describe('My Component', () => {
before(fakeAsync(() => {
myComponent.getAsyncData();
// Run async stuff.
tick();
// Uh oh, some code we don't control keeps adding async tasks!
discardFakeAsyncTimers();
// Gone now.
}));
it('should work', () => {
expect(myComponent.asyncResult).toBe('correct');
});
});
import 'zone.js';
export function discardFakeAsyncTimers() {
const fakeZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
fakeZoneSpec.pendingPeriodicTimers.length = 0;
fakeZoneSpec.pendingTimers.length = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment