Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77c2c6e

Browse files
authored
fix(jest-runtime): bind jest.isolateModulesAsync to this (#14083)
1 parent 25d59f3 commit 77c2c6e

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[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))
1212
- `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007))
1313
- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008))
14+
- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083))
1415
- `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036))
1516
- `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048))
1617

packages/jest-runtime/src/__tests__/runtime_jest_fn.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,56 @@ describe('Runtime', () => {
7676
expect(root.jest.isEnvironmentTornDown()).toBe(true);
7777
});
7878
});
79+
80+
describe('jest.isolateModules', () => {
81+
it('isolates the modules', async () => {
82+
const runtime = await createRuntime(__filename);
83+
const root = runtime.requireModule(runtime.__mockRootPath);
84+
root.jest.isolateModules(() => {
85+
const exports = runtime.requireModuleOrMock(
86+
runtime.__mockRootPath,
87+
'ModuleWithState',
88+
);
89+
expect(exports.getState()).toBe(1);
90+
exports.increment();
91+
expect(exports.getState()).toBe(2);
92+
});
93+
94+
root.jest.isolateModules(() => {
95+
const exports = runtime.requireModuleOrMock(
96+
runtime.__mockRootPath,
97+
'ModuleWithState',
98+
);
99+
expect(exports.getState()).toBe(1);
100+
exports.increment();
101+
expect(exports.getState()).toBe(2);
102+
});
103+
});
104+
});
105+
106+
describe('jest.isolateModulesAsync', () => {
107+
it('isolates the modules', async () => {
108+
const runtime = await createRuntime(__filename);
109+
const root = runtime.requireModule(runtime.__mockRootPath);
110+
await root.jest.isolateModulesAsync(async () => {
111+
const exports = runtime.requireModuleOrMock(
112+
runtime.__mockRootPath,
113+
'ModuleWithState',
114+
);
115+
expect(exports.getState()).toBe(1);
116+
exports.increment();
117+
expect(exports.getState()).toBe(2);
118+
});
119+
120+
await root.jest.isolateModulesAsync(async () => {
121+
const exports = runtime.requireModuleOrMock(
122+
runtime.__mockRootPath,
123+
'ModuleWithState',
124+
);
125+
expect(exports.getState()).toBe(1);
126+
exports.increment();
127+
expect(exports.getState()).toBe(2);
128+
});
129+
});
130+
});
79131
});

packages/jest-runtime/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,9 @@ export default class Runtime {
21972197
this.isolateModules(fn);
21982198
return jestObject;
21992199
};
2200+
const isolateModulesAsync = (fn: () => Promise<void>): Promise<void> => {
2201+
return this.isolateModulesAsync(fn);
2202+
};
22002203
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
22012204
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
22022205
const mocked =
@@ -2303,7 +2306,7 @@ export default class Runtime {
23032306
isEnvironmentTornDown: () => this.isTornDown,
23042307
isMockFunction: this._moduleMocker.isMockFunction,
23052308
isolateModules,
2306-
isolateModulesAsync: this.isolateModulesAsync,
2309+
isolateModulesAsync,
23072310
mock,
23082311
mocked,
23092312
now: () => _getFakeTimers().now(),

0 commit comments

Comments
 (0)