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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `[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))

Expand Down
52 changes: 52 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_jest_fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would fail before

exports.increment();
expect(exports.getState()).toBe(2);
});
});
});
});
5 changes: 4 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,9 @@ export default class Runtime {
this.isolateModules(fn);
return jestObject;
};
const isolateModulesAsync = (fn: () => Promise<void>): Promise<void> => {
return this.isolateModulesAsync(fn);
};
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
const mocked =
Expand Down Expand Up @@ -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(),
Expand Down