Skip to content

Commit

Permalink
feat(jest-runtime): expose isEnvironmentTornDown variable (#13741)
Browse files Browse the repository at this point in the history
  • Loading branch information
jomendez committed Jan 15, 2023
1 parent 7b33879 commit 73d7c1d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-environment/src/index.ts
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_jest_fn.js
Expand Up @@ -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);
});
});
});
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-types/__typetests__/jest.test.ts
Expand Up @@ -587,3 +587,6 @@ expectError(jest.setTimeout());

expectType<number>(jest.getSeed());
expectError(jest.getSeed(123));

expectType<boolean>(jest.isEnvironmentTornDown());
expectError(jest.isEnvironmentTornDown(123));

0 comments on commit 73d7c1d

Please sign in to comment.