Skip to content

Commit

Permalink
feat(jest-runtime): expose isTornDown variable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pepe authored and pepe committed Jan 7, 2023
1 parent fb2de8a commit 253d93d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

### Performance

## 29.3.2

### Features

- `[jest-runtime]` Expose `isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698))

## 29.3.1

### 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;
/**
* A flag that indicates whether the Jest environment has been torn down.
* @example
* if (jest.isEnvironmentTornDown()) {
* // The Jest environment has been torn down, so stop doing work
* return;
* }
*/
isEnvironmentTornDown(): boolean;
/**
* Mocks a module with an auto-mocked version when it is being required.
*/
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 @@ -2224,6 +2224,7 @@ export default class Runtime {
return this._globalConfig.seed;
},
getTimerCount: () => _getFakeTimers().getTimerCount(),
isEnvironmentTornDown: () => this.isTornDown,
isMockFunction: this._moduleMocker.isMockFunction,
isolateModules,
mock,
Expand Down

0 comments on commit 253d93d

Please sign in to comment.