Skip to content

Commit ed30de8

Browse files
committed
Reapply "fix(jest-resolver): Allow core ESM modules to be mocked with prefix (#15774)"
This reverts commit f759edd.
1 parent ebfa31c commit ed30de8

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = `
4141
12 | module.exports = () => 'test';
4242
13 |
4343
44-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1117:17)
44+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1121:17)
4545
at Object.require (index.js:10:1)
4646
at Object.require (__tests__/index.js:10:20)"
4747
`;
@@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = `
7171
12 | module.exports = () => 'test';
7272
13 |
7373
74-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1117:17)
74+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1121:17)
7575
at Object.require (index.js:10:1)
7676
at Object.require (__tests__/index.js:10:20)"
7777
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Ran all test suites matching native-esm.test.js."
5050
5151
exports[`runs test with native mock ESM 1`] = `
5252
"Test Suites: 1 passed, 1 total
53-
Tests: 3 passed, 3 total
53+
Tests: 4 passed, 4 total
5454
Snapshots: 0 total
5555
Time: <<REPLACED>>
5656
Ran all test suites matching native-esm-mocks.test.js."

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ test('can mock transitive module', async () => {
3030
expect(importedMock.foo).toBe('bar');
3131
});
3232

33+
test('can mock transitive builtin module', async () => {
34+
jestObject.unstable_mockModule('node:fs', () => ({foo: 'bar'}));
35+
36+
const importedMock = await import('../fsReexport.js');
37+
38+
expect(Object.keys(importedMock)).toEqual(['foo']);
39+
expect(importedMock.foo).toBe('bar');
40+
});
41+
3342
test('can unmock module', async () => {
3443
jestObject.unstable_mockModule('../index.js', () => ({
3544
double: () => 1000,

e2e/native-esm/fsReexport.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
8+
export * from 'node:fs';

packages/jest-resolve/src/resolver.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ export default class Resolver {
652652
options: ResolveModuleConfig,
653653
): Promise<string | null> {
654654
if (this.isCoreModule(moduleName)) {
655-
return moduleName;
655+
return this.normalizeCoreModuleSpecifier(moduleName);
656656
}
657657
if (moduleName.startsWith('data:')) {
658658
return moduleName;
@@ -742,6 +742,11 @@ export default class Resolver {
742742
moduleName: string,
743743
options?: Pick<ResolveModuleConfig, 'conditions'>,
744744
): string | null {
745+
// Strip core module scheme if necessary.
746+
if (this.isCoreModule(moduleName)) {
747+
return this.normalizeCoreModuleSpecifier(moduleName);
748+
}
749+
745750
const dirname = path.dirname(from);
746751

747752
const {extensions, moduleDirectory, paths} = this._prepareForResolution(

0 commit comments

Comments
 (0)