Skip to content

Commit 7a2c4af

Browse files
test: add integration test for unstable_unmockModule
Closes #15079
1 parent f114dfb commit 7a2c4af

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ Ran all test suites matching native-esm-wasm.test.js."
4242
4343
exports[`runs test with native ESM 1`] = `
4444
"Test Suites: 1 passed, 1 total
45-
Tests: 33 passed, 33 total
45+
Tests: 31 passed, 31 total
4646
Snapshots: 0 total
4747
Time: <<REPLACED>>
4848
Ran all test suites matching native-esm.test.js."
4949
`;
5050
51+
exports[`runs test with native mock ESM 1`] = `
52+
"Test Suites: 1 passed, 1 total
53+
Tests: 3 passed, 3 total
54+
Snapshots: 0 total
55+
Time: <<REPLACED>>
56+
Ran all test suites matching native-esm-mocks.test.js."
57+
`;
58+
5159
exports[`support re-exports from CJS of core module 1`] = `
5260
"Test Suites: 1 passed, 1 total
5361
Tests: 1 passed, 1 total

e2e/__tests__/nativeEsm.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ test('runs test with native ESM', () => {
5151
expect(exitCode).toBe(0);
5252
});
5353

54+
test('runs test with native mock ESM', () => {
55+
const {exitCode, stderr, stdout} = runJest(
56+
DIR,
57+
['native-esm-mocks.test.js'],
58+
{
59+
nodeOptions: '--experimental-vm-modules --no-warnings',
60+
},
61+
);
62+
63+
const {summary} = extractSummary(stderr);
64+
65+
expect(summary).toMatchSnapshot();
66+
expect(stdout).toBe('');
67+
expect(exitCode).toBe(0);
68+
});
69+
5470
test('supports top-level await', () => {
5571
const {exitCode, stderr, stdout} = runJest(DIR, ['native-esm.tla.test.js'], {
5672
nodeOptions: '--experimental-vm-modules --no-warnings',
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
import {jest as jestObject} from '@jest/globals';
8+
9+
afterEach(() => {
10+
jestObject.resetModules();
11+
});
12+
13+
test('can mock module', async () => {
14+
jestObject.unstable_mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
15+
virtual: true,
16+
});
17+
18+
const importedMock = await import('../mockedModule.mjs');
19+
20+
expect(Object.keys(importedMock)).toEqual(['foo']);
21+
expect(importedMock.foo).toBe('bar');
22+
});
23+
24+
test('can mock transitive module', async () => {
25+
jestObject.unstable_mockModule('../index.js', () => ({foo: 'bar'}));
26+
27+
const importedMock = await import('../reexport.js');
28+
29+
expect(Object.keys(importedMock)).toEqual(['foo']);
30+
expect(importedMock.foo).toBe('bar');
31+
});
32+
33+
test('can unmock module', async () => {
34+
jestObject.unstable_mockModule('../index.js', () => ({
35+
double: () => 1000,
36+
}));
37+
38+
const importedMock = await import('../index.js');
39+
expect(importedMock.double()).toBe(1000);
40+
41+
jestObject.unstable_unmockModule('../index.js');
42+
43+
const importedMockAfterUnmock = await import('../index.js');
44+
expect(importedMockAfterUnmock.double(2)).toBe(4);
45+
});

e2e/native-esm/__tests__/native-esm.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,26 +224,6 @@ test('require of ESM should throw correct error', () => {
224224
);
225225
});
226226

227-
test('can mock module', async () => {
228-
jestObject.unstable_mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
229-
virtual: true,
230-
});
231-
232-
const importedMock = await import('../mockedModule.mjs');
233-
234-
expect(Object.keys(importedMock)).toEqual(['foo']);
235-
expect(importedMock.foo).toBe('bar');
236-
});
237-
238-
test('can mock transitive module', async () => {
239-
jestObject.unstable_mockModule('../index.js', () => ({foo: 'bar'}));
240-
241-
const importedMock = await import('../reexport.js');
242-
243-
expect(Object.keys(importedMock)).toEqual(['foo']);
244-
expect(importedMock.foo).toBe('bar');
245-
});
246-
247227
test('supports imports using "node:" prefix', () => {
248228
expect(dns).toBe(prefixDns);
249229
});

0 commit comments

Comments
 (0)