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 33c068e9..9b18113f 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 @@ -138,9 +138,20 @@ describe('AngularContext', () => { }); describe('next test run', () => { - function runTest(): void { - // ensure that the _second_ run of this test does not throw the error "There is already another AngularContext in use (or it was not cleaned up)" + function runInitTest(): void { + class BadInitContext extends AngularContext { + protected override init(): void { + super.init(); + throw new Error('mess up init'); + } + } + const ctx = new BadInitContext(); + expect(() => { + ctx.run(noop); + }).toThrowError('mess up init'); + } + function runCleanupTest(): void { const ctx = new AngularContext(); expect(() => { ctx.run(() => { @@ -151,12 +162,20 @@ describe('AngularContext', () => { }).toThrowError('mess up cleanup'); } + it('is OK when throwing an error during init', () => { + runInitTest(); + }); + + it('is OK when throwing an error during init', () => { + runInitTest(); + }); + it('is OK when throwing an error during cleanup', () => { - runTest(); + runCleanupTest(); }); it('is OK when throwing an error during cleanup', () => { - runTest(); + runCleanupTest(); }); }); }); 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 0dad5ed2..3df1455c 100644 --- a/projects/ng-dev/src/lib/angular-context/angular-context.ts +++ b/projects/ng-dev/src/lib/angular-context/angular-context.ts @@ -140,21 +140,21 @@ export class AngularContext { * 4. `this.cleanUp()` */ run(test: () => Promise | void): void { - this.#runWithMockedTime(() => { - this.init(); - try { - // eslint-disable-next-line @typescript-eslint/no-floating-promises - test(); - this.tick(); - this.verifyPostTestConditions(); - } finally { + try { + this.#runWithMockedTime(() => { + this.init(); try { - this.cleanUp(); + // eslint-disable-next-line @typescript-eslint/no-floating-promises + test(); + this.tick(); + this.verifyPostTestConditions(); } finally { - AngularContext.#current = undefined; + this.cleanUp(); } - } - }); + }); + } finally { + AngularContext.#current = undefined; + } } /**