Skip to content

Commit

Permalink
fix(ng-dev): AngularContext cleans up after itself better in error …
Browse files Browse the repository at this point in the history
…situations, to avoid all future tests failing "There is already another AngularContext in use (or it was not cleaned up)"

closes #98
  • Loading branch information
ersimont committed Jan 16, 2023
1 parent 49b195d commit 81cbd5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
23 changes: 23 additions & 0 deletions projects/ng-dev/src/lib/angular-context/angular-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
7 changes: 5 additions & 2 deletions projects/ng-dev/src/lib/angular-context/angular-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ export class AngularContext {
this.tick();
this.verifyPostTestConditions();
} finally {
this.cleanUp();
try {
this.cleanUp();
} finally {
AngularContext.#current = undefined;
}
}
});
}
Expand Down Expand Up @@ -247,6 +251,5 @@ export class AngularContext {
protected cleanUp(): void {
discardPeriodicTasks();
flush();
AngularContext.#current = undefined;
}
}

0 comments on commit 81cbd5c

Please sign in to comment.