Skip to content

Commit 73d7c1d

Browse files
authored
feat(jest-runtime): expose isEnvironmentTornDown variable (#13741)
1 parent 7b33879 commit 73d7c1d

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `[jest-haste-map]` ignore Sapling vcs directories (`.sl/`) ([#13674](https://github.com/facebook/jest/pull/13674))
99
- `[jest-resolve]` Support subpath imports ([#13705](https://github.com/facebook/jest/pull/13705), [#13723](https://github.com/facebook/jest/pull/13723))
1010
- `[jest-runtime]` Add `jest.isolateModulesAsync` for scoped module initialization of asynchronous functions ([#13680](https://github.com/facebook/jest/pull/13680))
11+
- `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698))
1112
- `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700))
1213

1314
### Fixes

packages/jest-environment/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ export interface Jest {
172172
* local module state doesn't conflict between tests.
173173
*/
174174
isolateModules(fn: () => void): Jest;
175+
/**
176+
* Returns `true` if test environment has been torn down.
177+
* @example
178+
* if (jest.isEnvironmentTornDown()) {
179+
* // The Jest environment has been torn down, so stop doing work
180+
* return;
181+
* }
182+
*/
183+
isEnvironmentTornDown(): boolean;
175184
/**
176185
* `jest.isolateModulesAsync()` is the equivalent of `jest.isolateModules()`, but for
177186
* async functions to be wrapped. The caller is expected to `await` the completion of

packages/jest-runtime/src/__tests__/runtime_jest_fn.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@ describe('Runtime', () => {
6666
expect(mock2).not.toHaveBeenCalled();
6767
});
6868
});
69+
70+
describe('jest.isEnvironmentTornDown()', () => {
71+
it('should be set to true when the environment is torn down', async () => {
72+
const runtime = await createRuntime(__filename);
73+
const root = runtime.requireModule(runtime.__mockRootPath);
74+
expect(root.jest.isEnvironmentTornDown()).toBe(false);
75+
runtime.teardown();
76+
expect(root.jest.isEnvironmentTornDown()).toBe(true);
77+
});
78+
});
6979
});

packages/jest-runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,7 @@ export default class Runtime {
22372237
return this._globalConfig.seed;
22382238
},
22392239
getTimerCount: () => _getFakeTimers().getTimerCount(),
2240+
isEnvironmentTornDown: () => this.isTornDown,
22402241
isMockFunction: this._moduleMocker.isMockFunction,
22412242
isolateModules,
22422243
isolateModulesAsync: this.isolateModulesAsync,

packages/jest-types/__typetests__/jest.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,6 @@ expectError(jest.setTimeout());
587587

588588
expectType<number>(jest.getSeed());
589589
expectError(jest.getSeed(123));
590+
591+
expectType<boolean>(jest.isEnvironmentTornDown());
592+
expectError(jest.isEnvironmentTornDown(123));

0 commit comments

Comments
 (0)