diff --git a/projects/ng-dev/src/lib/angular-context/angular-context.spec.ts b/projects/ng-dev/src/lib/angular-context/angular-context.spec.ts index 2bbf2fe3..f52bb03c 100644 --- a/projects/ng-dev/src/lib/angular-context/angular-context.spec.ts +++ b/projects/ng-dev/src/lib/angular-context/angular-context.spec.ts @@ -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()', () => { diff --git a/projects/ng-dev/src/lib/angular-context/angular-context.ts b/projects/ng-dev/src/lib/angular-context/angular-context.ts index 39e9623e..b27b057c 100644 --- a/projects/ng-dev/src/lib/angular-context/angular-context.ts +++ b/projects/ng-dev/src/lib/angular-context/angular-context.ts @@ -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();