Skip to content

Commit

Permalink
feat(ng-dev): AngularContext flushes pending timeouts automatically…
Browse files Browse the repository at this point in the history
… at the end of each test (instead of throwing the error "X timer(s) still in the queue.")

Closes #21
  • Loading branch information
ersimont committed Jan 7, 2021
1 parent 06be1e5 commit 33d890a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Expand Up @@ -265,6 +265,17 @@ describe('AngularContextNext', () => {
// No error: "1 periodic timer(s) still in the queue."
.not.toThrowError();
});

it('flushes pending timeouts', () => {
const ctx = new AngularContextNext();
expect(() => {
ctx.run(() => {
setTimeout(noop, 1);
});
})
// No error: "1 timer(s) still in the queue."
.not.toThrowError();
});
});
});

Expand Down
Expand Up @@ -12,6 +12,7 @@ import {
import {
discardPeriodicTasks,
fakeAsync,
flush,
flushMicrotasks,
TestBed,
TestModuleMetadata,
Expand Down Expand Up @@ -43,8 +44,7 @@ export function extendMetadata(
* - Gives control over the simulated date & time with a single line of code.
* - Automatically includes {@link HttpClientTestingModule} to stub network requests without additional setup.
* - Always verifies no unexpected http requests were made during a test.
* - Always discards periodic tasks at the end of each test to automatically
* avoid an error from the `fakeAsync` zone.
* - Always discards periodic tasks and flushes pending timers at the end of each test to automatically avoid the error "X timer(s) still in the queue".
*
* Why does the class name end with "Next"? This replaces the old `AngularContext`, but it's a breaking change so this gives people some time to transition over. Eventually the old one will be removed and this will be renamed to `AngularContext`.
*
Expand Down Expand Up @@ -183,10 +183,11 @@ export class AngularContextNext {
}

/**
* Performs any cleanup needed at the end of each test. This base implementation calls [discardPeriodicTasks]{@linkcode https://angular.io/api/core/testing/discardPeriodicTasks} to avoid an error from the `fakeAsync` zone.
* Performs any cleanup needed at the end of each test. This base implementation calls {@linkcode https://angular.io/api/core/testing/discardPeriodicTasks|discardPeriodicTasks} and [flush]{https://angular.io/api/core/testing/flush|flush} to avoid an error from the `fakeAsync` zone.
*/
protected cleanUp(): void {
discardPeriodicTasks();
flush();
}

private runWithMockedTime(test: VoidFunction): void {
Expand Down
Expand Up @@ -19,8 +19,8 @@ import {
} from '@angular/core/testing';
import { assert, convertTime } from '@s-libs/js-core';
import { clone, forOwn, isFunction } from '@s-libs/micro-dash';
import { FakeAsyncHarnessEnvironment } from './fake-async-harness-environment';
import { Synchronized } from '../synchronize';
import { FakeAsyncHarnessEnvironment } from './fake-async-harness-environment';

/** @hidden */
export function extendMetadata(
Expand Down

0 comments on commit 33d890a

Please sign in to comment.