diff --git a/projects/ng-dev/README.md b/projects/ng-dev/README.md index 42714583..3c03b61f 100644 --- a/projects/ng-dev/README.md +++ b/projects/ng-dev/README.md @@ -10,7 +10,7 @@ yarn add -D @s-libs/ng-dev @s-libs/ng-core @s-libs/rxjs-core @s-libs/js-core @s- ## TSLint Config -This library comes with a predefined `tslint.json` that track's the angular cli's config with these changes that we have found useful: +This library comes with a predefined `tslint.json` that tracks the angular cli's config with these changes that we have found useful: - Disables rules that conflict with Prettier (via [tslint-config-prettier](https://github.com/prettier/tslint-config-prettier)) - Allows using the `Function` type. Some of our libraries deal a lot with utilities that operate on functions, and using this type is very handy. diff --git a/projects/ng-dev/src/lib/test-context/angular-context.spec.ts b/projects/ng-dev/src/lib/test-context/angular-context.spec.ts index 64e115d8..855f5a71 100644 --- a/projects/ng-dev/src/lib/test-context/angular-context.spec.ts +++ b/projects/ng-dev/src/lib/test-context/angular-context.spec.ts @@ -201,6 +201,14 @@ describe('AngularContext', () => { expect(promiseResolvedBeforeChangeDetection).toBe(true); }); }); + + it('advances `performance.now()` as well', () => { + ctx.run(() => { + const start = performance.now(); + ctx.tick(10); + expect(performance.now()).toBe(start + 10); + }); + }); }); describe('.init()', () => { diff --git a/projects/ng-dev/src/lib/test-context/angular-context.ts b/projects/ng-dev/src/lib/test-context/angular-context.ts index 7a1c84df..ef63fe6e 100644 --- a/projects/ng-dev/src/lib/test-context/angular-context.ts +++ b/projects/ng-dev/src/lib/test-context/angular-context.ts @@ -128,9 +128,7 @@ export class AngularContext { options = optionsOrTest; } - jasmine.clock().install(); - fakeAsync(() => { - jasmine.clock().mockDate(this.startTime); + this.runWithMockedTime(() => { assert(test); this.init(options); @@ -140,8 +138,7 @@ export class AngularContext { } finally { this.cleanUp(); } - })(); - jasmine.clock().uninstall(); + }); } /** @@ -216,4 +213,19 @@ export class AngularContext { protected cleanUp(): void { discardPeriodicTasks(); } + + private runWithMockedTime(test: VoidFunction): void { + // https://github.com/angular/angular/issues/31677#issuecomment-573139551 + const now = performance.now; + spyOn(performance, 'now').and.callFake(() => Date.now()); + + jasmine.clock().install(); + fakeAsync(() => { + jasmine.clock().mockDate(this.startTime); + test(); + })(); + jasmine.clock().uninstall(); + + performance.now = now; + } } diff --git a/projects/ng-dev/src/lib/test-context/index.ts b/projects/ng-dev/src/lib/test-context/index.ts index 9731b58e..c38e1abd 100644 --- a/projects/ng-dev/src/lib/test-context/index.ts +++ b/projects/ng-dev/src/lib/test-context/index.ts @@ -1,3 +1,3 @@ export { AngularContext } from './angular-context'; -export { ComponentContext } from './component-context'; +export { ComponentContext, ComponentContextInit } from './component-context'; export { Synchronized } from './synchronize';