From 805534e484a6fee02eeace2df0232d48c7bedc7e Mon Sep 17 00:00:00 2001 From: pepe Date: Tue, 27 Dec 2022 19:52:52 -0500 Subject: [PATCH] feat(jest-runtime): expose isTornDown variable This commit adds a new feature to the Jest runtime package that exposes the `isTornDown` variable as a way to check if the Jest environment has been torn down programmatically. The `isTornDown` variable is a read-only boolean that is set to `true` when the environment is torn down and `false` otherwise. The commit also adds documentation for the `isTornDown` variable to the Jest runtime module. Closes #13640 --- packages/jest-environment/src/index.ts | 9 +++++++++ packages/jest-runtime/src/index.ts | 1 + 2 files changed, 10 insertions(+) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index df50111e37f5..f871458b8a49 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -172,6 +172,15 @@ export interface Jest { * local module state doesn't conflict between tests. */ isolateModules(fn: () => void): Jest; + /** + * A flag that indicates whether the Jest environment has been torn down. + * @example + * if (jest.isTornDown) { + * // The Jest environment has been torn down, so stop doing work + * return; + * } + */ + isTornDown: boolean; /** * Mocks a module with an auto-mocked version when it is being required. */ diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c278d77052c5..d001c7f64438 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2225,6 +2225,7 @@ export default class Runtime { }, getTimerCount: () => _getFakeTimers().getTimerCount(), isMockFunction: this._moduleMocker.isMockFunction, + isTornDown: this.isTornDown, isolateModules, mock, mocked,