From 81cbd5c43344bfaf17c51106bf66d08353ad7ee0 Mon Sep 17 00:00:00 2001 From: ersimont <8042088+ersimont@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:02:41 -0500 Subject: [PATCH] fix(ng-dev): `AngularContext` cleans up after itself better in error situations, to avoid all future tests failing "There is already another AngularContext in use (or it was not cleaned up)" closes #98 --- .../angular-context/angular-context.spec.ts | 23 +++++++++++++++++++ .../lib/angular-context/angular-context.ts | 7 ++++-- 2 files changed, 28 insertions(+), 2 deletions(-) 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 89ca7d59..9c4d7711 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 @@ -136,6 +136,29 @@ describe('AngularContext', () => { }); }).toThrowError(); }); + + 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)" + + const ctx = new AngularContext(); + expect(() => { + ctx.run(() => { + setTimeout(() => { + throw new Error('mess up cleanup'); + }, 1); + }); + }).toThrowError('mess up cleanup'); + } + + it('is OK when throwing an error during cleanup', () => { + runTest(); + }); + + it('is OK when throwing an error during cleanup', () => { + runTest(); + }); + }); }); describe('.inject()', () => { 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 4a842432..dbe046e3 100644 --- a/projects/ng-dev/src/lib/angular-context/angular-context.ts +++ b/projects/ng-dev/src/lib/angular-context/angular-context.ts @@ -148,7 +148,11 @@ export class AngularContext { this.tick(); this.verifyPostTestConditions(); } finally { - this.cleanUp(); + try { + this.cleanUp(); + } finally { + AngularContext.#current = undefined; + } } }); } @@ -247,6 +251,5 @@ export class AngularContext { protected cleanUp(): void { discardPeriodicTasks(); flush(); - AngularContext.#current = undefined; } }