Skip to content

Commit

Permalink
fix(ng-dev): AngularContext cleans up after itself even better in e…
Browse files Browse the repository at this point in the history
…rror situations, to avoid all future tests failing "There is already another AngularContext in use (or it was not cleaned up)"
  • Loading branch information
ersimont committed Jan 21, 2023
1 parent 0b6bc02 commit f208815
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
27 changes: 23 additions & 4 deletions projects/ng-dev/src/lib/angular-context/angular-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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();
});
});
});
Expand Down
24 changes: 12 additions & 12 deletions projects/ng-dev/src/lib/angular-context/angular-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ export class AngularContext {
* 4. `this.cleanUp()`
*/
run(test: () => Promise<void> | 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;
}
}

/**
Expand Down

0 comments on commit f208815

Please sign in to comment.