diff --git a/CHANGELOG.md b/CHANGELOG.md index 067791482e22..1b1d29300bbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-haste-map]` ignore Sapling vcs directories (`.sl/`) ([#13674](https://github.com/facebook/jest/pull/13674)) - `[jest-resolve]` Support subpath imports ([#13705](https://github.com/facebook/jest/pull/13705), [#13723](https://github.com/facebook/jest/pull/13723)) - `[jest-runtime]` Add `jest.isolateModulesAsync` for scoped module initialization of asynchronous functions ([#13680](https://github.com/facebook/jest/pull/13680)) +- `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) - `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700)) ### Fixes diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 8ec2980e414a..bc542ef27144 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; + /** + * Returns `true` if test environment has been torn down. + * @example + * if (jest.isEnvironmentTornDown()) { + * // The Jest environment has been torn down, so stop doing work + * return; + * } + */ + isEnvironmentTornDown(): boolean; /** * `jest.isolateModulesAsync()` is the equivalent of `jest.isolateModules()`, but for * async functions to be wrapped. The caller is expected to `await` the completion of diff --git a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js index a85322f97644..0bb1a0667b3d 100644 --- a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js +++ b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js @@ -66,4 +66,14 @@ describe('Runtime', () => { expect(mock2).not.toHaveBeenCalled(); }); }); + + describe('jest.isEnvironmentTornDown()', () => { + it('should be set to true when the environment is torn down', async () => { + const runtime = await createRuntime(__filename); + const root = runtime.requireModule(runtime.__mockRootPath); + expect(root.jest.isEnvironmentTornDown()).toBe(false); + runtime.teardown(); + expect(root.jest.isEnvironmentTornDown()).toBe(true); + }); + }); }); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 2729651f622a..6836cde5e52e 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2237,6 +2237,7 @@ export default class Runtime { return this._globalConfig.seed; }, getTimerCount: () => _getFakeTimers().getTimerCount(), + isEnvironmentTornDown: () => this.isTornDown, isMockFunction: this._moduleMocker.isMockFunction, isolateModules, isolateModulesAsync: this.isolateModulesAsync, diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 392fdfff2cb4..6f9eafa2173d 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -587,3 +587,6 @@ expectError(jest.setTimeout()); expectType(jest.getSeed()); expectError(jest.getSeed(123)); + +expectType(jest.isEnvironmentTornDown()); +expectError(jest.isEnvironmentTornDown(123));