From 805534e484a6fee02eeace2df0232d48c7bedc7e Mon Sep 17 00:00:00 2001 From: pepe Date: Tue, 27 Dec 2022 19:52:52 -0500 Subject: [PATCH 1/7] feat(jest-runtime): expose isTornDown variable 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 --- packages/jest-environment/src/index.ts | 9 +++++++++ packages/jest-runtime/src/index.ts | 1 + 2 files changed, 10 insertions(+) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index df50111e37f5..f871458b8a49 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; + /** + * A flag that indicates whether the Jest environment has been torn down. + * @example + * if (jest.isTornDown) { + * // The Jest environment has been torn down, so stop doing work + * return; + * } + */ + isTornDown: boolean; /** * Mocks a module with an auto-mocked version when it is being required. */ diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c278d77052c5..d001c7f64438 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2225,6 +2225,7 @@ export default class Runtime { }, getTimerCount: () => _getFakeTimers().getTimerCount(), isMockFunction: this._moduleMocker.isMockFunction, + isTornDown: this.isTornDown, isolateModules, mock, mocked, From 253d93de212a9c626bb4d48515976bde5227b155 Mon Sep 17 00:00:00 2001 From: pepe Date: Tue, 27 Dec 2022 19:52:52 -0500 Subject: [PATCH 2/7] feat(jest-runtime): expose isTornDown variable 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 --- CHANGELOG.md | 6 ++++++ packages/jest-environment/src/index.ts | 9 +++++++++ .../src/__tests__/runtime_jest_fn.js | 17 +++++++++++++++++ packages/jest-runtime/src/index.ts | 1 + 4 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f7984aa970..c7d26944a8cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index df50111e37f5..9f35a77eea4f 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; + /** + * 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. */ diff --git a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js index a85322f97644..f793f131c40f 100644 --- a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js +++ b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js @@ -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); + }); + }); }); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c278d77052c5..03a80ff3cf05 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2224,6 +2224,7 @@ export default class Runtime { return this._globalConfig.seed; }, getTimerCount: () => _getFakeTimers().getTimerCount(), + isEnvironmentTornDown: () => this.isTornDown, isMockFunction: this._moduleMocker.isMockFunction, isolateModules, mock, From d364e0e0228c4261bece52892bfdbb9516130941 Mon Sep 17 00:00:00 2001 From: pepe Date: Fri, 6 Jan 2023 23:16:38 -0500 Subject: [PATCH 3/7] added a new feature to the Jest runtime package that exposes the isEnvironmentTornDown variable as a way to check if the Jest environment has been torndown --- packages/jest-runtime/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 8fa80618f564..40e3a398c942 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2252,7 +2252,6 @@ export default class Runtime { getTimerCount: () => _getFakeTimers().getTimerCount(), isEnvironmentTornDown: () => this.isTornDown, isMockFunction: this._moduleMocker.isMockFunction, - isTornDown: this.isTornDown, isolateModules, isolateModulesAsync: this.isolateModulesAsync, mock, From 5bd9de0b128d7744faf21aa18b45a8ec70f44793 Mon Sep 17 00:00:00 2001 From: pepe Date: Mon, 9 Jan 2023 00:14:09 -0500 Subject: [PATCH 4/7] added type test, updated CHANGELOG.md, updated the description of the function, removed the word flag --- CHANGELOG.md | 8 ++------ packages/jest-environment/src/index.ts | 2 +- packages/jest-types/__typetests__/jest.test.ts | 3 +++ 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb385273326..026aa15c6392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-runtime]` Expose `isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) + - `[expect, @jest/expect-utils]` Support custom equality testers ([#13654](https://github.com/facebook/jest/pull/13654)) - `[jest-config, jest-worker]` Use `os.availableParallelism` if available to calculate number of workers to spawn ([#13738](https://github.com/facebook/jest/pull/13738)) - `[@jest/globals, jest-mock]` Add `jest.replaceProperty()` that replaces property value ([#13496](https://github.com/facebook/jest/pull/13496)) @@ -27,12 +29,6 @@ ### Performance -## 29.3.2 - -### Features - -- `[jest-runtime]` Expose `isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) - ## 29.3.1 ### Fixes diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index fd7cd1b4f533..bc542ef27144 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -173,7 +173,7 @@ export interface Jest { */ isolateModules(fn: () => void): Jest; /** - * A flag that indicates whether the Jest environment has been torn down. + * Returns `true` if test environment has been torn down. * @example * if (jest.isEnvironmentTornDown()) { * // The Jest environment has been torn down, so stop doing work diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 392fdfff2cb4..c8d68a70a9c8 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)); From c27fb5f4314b12eb87b152469e25cd3a554191d0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 9 Jan 2023 10:46:38 +0100 Subject: [PATCH 5/7] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 026aa15c6392..1b1d29300bbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,13 @@ ### Features -- `[jest-runtime]` Expose `isEnvironmentTornDown` function ([#13698](https://github.com/facebook/jest/pull/13698)) - - `[expect, @jest/expect-utils]` Support custom equality testers ([#13654](https://github.com/facebook/jest/pull/13654)) - `[jest-config, jest-worker]` Use `os.availableParallelism` if available to calculate number of workers to spawn ([#13738](https://github.com/facebook/jest/pull/13738)) - `[@jest/globals, jest-mock]` Add `jest.replaceProperty()` that replaces property value ([#13496](https://github.com/facebook/jest/pull/13496)) - `[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 From 6883bcc95a3c567992035e6d9065f47e43351b3c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 15 Jan 2023 11:34:16 +0100 Subject: [PATCH 6/7] Update packages/jest-types/__typetests__/jest.test.ts Co-authored-by: Tom Mrazauskas --- packages/jest-types/__typetests__/jest.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index c8d68a70a9c8..6f9eafa2173d 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -589,4 +589,4 @@ expectType(jest.getSeed()); expectError(jest.getSeed(123)); expectType(jest.isEnvironmentTornDown()); -expectError(jest.isEnvironmentTornDown(123)); +expectError(jest.isEnvironmentTornDown(123)); From de6ade0e836f07c0d5991823c7d101cf58e62846 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 15 Jan 2023 11:36:13 +0100 Subject: [PATCH 7/7] remove extraneous test --- packages/jest-runtime/src/__tests__/runtime_jest_fn.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js index f793f131c40f..0bb1a0667b3d 100644 --- a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js +++ b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js @@ -68,13 +68,6 @@ describe('Runtime', () => { }); 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);