Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-runtime): expose isEnvironmentTornDown variable #13741

Merged
merged 11 commits into from
Jan 15, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
17 changes: 17 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_jest_fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,21 @@ describe('Runtime', () => {
expect(mock2).not.toHaveBeenCalled();
});
});

describe('jest.isEnvironmentTornDown()', () => {
it('should be set to false when the environment is not torn down', async () => {
const runtime = await createRuntime(__filename);
const root = runtime.requireModule(runtime.__mockRootPath);
runtime['isTornDown'] = false;
expect(root.jest.isEnvironmentTornDown()).toBe(false);
});

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,6 @@ expectError(jest.setTimeout());

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

expectType<boolean>(jest.isEnvironmentTornDown());
expectError<boolean>(jest.isEnvironmentTornDown(123));
SimenB marked this conversation as resolved.
Show resolved Hide resolved