Skip to content

Commit

Permalink
feat(ng-dev): provide a nice error message why trying to use `Angular…
Browse files Browse the repository at this point in the history
…Context.tick()` outside `.run()`

Closes #29
  • Loading branch information
ersimont committed Jul 13, 2021
1 parent d4c6037 commit 396af3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ describe('AngularContext', () => {
expect(performance.now()).toBe(start + 10);
});
});

it('gives a nice error message when you try to use it outside `run()`', () => {
const ctx = new AngularContext();
expect(() => {
ctx.tick();
}).toThrowError(
'.tick() only works inside the .run() callback (because it needs to be in a fakeAsync zone)',
);
});
});

describe('.verifyPostTestConditions()', () => {
Expand Down
6 changes: 6 additions & 0 deletions projects/ng-dev/src/lib/angular-context/angular-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ export class AngularContext {
* @param unit The unit of time `amount` represents. Accepts anything described in `@s-libs/s-core`'s [TimeUnit]{@linkcode https://simontonsoftware.github.io/s-js-utils/typedoc/enums/timeunit.html} enum.
*/
tick(amount = 0, unit = 'ms'): void {
if (!Zone.current.get('FakeAsyncTestZoneSpec')) {
throw new Error(
'.tick() only works inside the .run() callback (because it needs to be in a fakeAsync zone)',
);
}

// To simulate real life, trigger change detection before processing macro tasks. To further simulate real life, wait until the micro task queue is empty.
flushMicrotasks();
this.runChangeDetection();
Expand Down

0 comments on commit 396af3c

Please sign in to comment.