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(),