From e6c7f992698ddbdaeb9520d51d212eb04ac7e90c Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Tue, 18 Apr 2023 17:14:43 +0100 Subject: [PATCH 1/2] fix(jest-runtime): bind `jest.isolateModulesAsync` to `this` --- CHANGELOG.md | 1 + .../src/__tests__/runtime_jest_fn.js | 52 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 5 +- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 309e3d342619..0f71b8843154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083)) - `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054)) - `[jest-core]` Always use workers in watch mode to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059)). - `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) diff --git a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js index 343c34fa8ea6..c377132cd6f1 100644 --- a/packages/jest-runtime/src/__tests__/runtime_jest_fn.js +++ b/packages/jest-runtime/src/__tests__/runtime_jest_fn.js @@ -76,4 +76,56 @@ describe('Runtime', () => { expect(root.jest.isEnvironmentTornDown()).toBe(true); }); }); + + describe('jest.isolateModules', () => { + it('isolates the modules', async () => { + const runtime = await createRuntime(__filename); + const root = runtime.requireModule(runtime.__mockRootPath); + root.jest.isolateModules(() => { + const exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(1); + exports.increment(); + expect(exports.getState()).toBe(2); + }); + + root.jest.isolateModules(() => { + const exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(1); + exports.increment(); + expect(exports.getState()).toBe(2); + }); + }); + }); + + describe('jest.isolateModulesAsync', () => { + it('isolates the modules', async () => { + const runtime = await createRuntime(__filename); + const root = runtime.requireModule(runtime.__mockRootPath); + await root.jest.isolateModulesAsync(async () => { + const exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(1); + exports.increment(); + expect(exports.getState()).toBe(2); + }); + + await root.jest.isolateModulesAsync(async () => { + const exports = runtime.requireModuleOrMock( + runtime.__mockRootPath, + 'ModuleWithState', + ); + expect(exports.getState()).toBe(1); + exports.increment(); + expect(exports.getState()).toBe(2); + }); + }); + }); }); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 5fa5186fd20b..f9945f8e7f72 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2197,6 +2197,9 @@ export default class Runtime { this.isolateModules(fn); return jestObject; }; + const isolateModulesAsync = (fn: () => Promise): Promise => { + return this.isolateModulesAsync(fn); + }; const fn = this._moduleMocker.fn.bind(this._moduleMocker); const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker); const mocked = @@ -2303,7 +2306,7 @@ export default class Runtime { isEnvironmentTornDown: () => this.isTornDown, isMockFunction: this._moduleMocker.isMockFunction, isolateModules, - isolateModulesAsync: this.isolateModulesAsync, + isolateModulesAsync, mock, mocked, now: () => _getFakeTimers().now(), From 70ea9309972669f0a0e07d3e855db0b752d4798f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 19 Apr 2023 12:28:48 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f71b8843154..8ccc315b3bd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,12 @@ ### Fixes -- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083)) - `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054)) - `[jest-core]` Always use workers in watch mode to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059)). - `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) - `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007)) - `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008)) +- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083)) - `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036)) - `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048))