Skip to content

Commit d6fb0d8

Browse files
authored
fix: resolve bundled modules from correct location (#11493)
1 parent bba34bd commit d6fb0d8

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
### Performance
1010

11+
## 27.0.4
12+
13+
### Fixes
14+
15+
- `[jest-config, jest-resolve]` Pass in `require.resolve` to resolvers to resolve from correct base ([#11493](https://github.com/facebook/jest/pull/11493))
16+
1117
## 27.0.3
1218

1319
### Fixes

packages/jest-config/src/normalize.ts

+5
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ export default async function normalize(
607607
}
608608

609609
options.testEnvironment = resolveTestEnvironment({
610+
requireResolveFunction: require.resolve,
610611
rootDir: options.rootDir,
611612
testEnvironment:
612613
options.testEnvironment ||
@@ -745,6 +746,7 @@ export default async function normalize(
745746
option &&
746747
resolveRunner(newOptions.resolver, {
747748
filePath: option,
749+
requireResolveFunction: require.resolve,
748750
rootDir: options.rootDir,
749751
});
750752
}
@@ -1016,6 +1018,7 @@ export default async function normalize(
10161018
config: {},
10171019
path: resolveWatchPlugin(newOptions.resolver, {
10181020
filePath: watchPlugin,
1021+
requireResolveFunction: require.resolve,
10191022
rootDir: options.rootDir,
10201023
}),
10211024
};
@@ -1024,6 +1027,7 @@ export default async function normalize(
10241027
config: watchPlugin[1] || {},
10251028
path: resolveWatchPlugin(newOptions.resolver, {
10261029
filePath: watchPlugin[0],
1030+
requireResolveFunction: require.resolve,
10271031
rootDir: options.rootDir,
10281032
}),
10291033
};
@@ -1058,6 +1062,7 @@ export default async function normalize(
10581062
newOptions.testSequencer = resolveSequencer(newOptions.resolver, {
10591063
filePath:
10601064
options.testSequencer || require.resolve(DEFAULT_CONFIG.testSequencer),
1065+
requireResolveFunction: require.resolve,
10611066
rootDir: options.rootDir,
10621067
});
10631068

packages/jest-resolve/src/utils.ts

+41-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ const resolveWithPrefix = (
4040
humanOptionName,
4141
optionName,
4242
prefix,
43+
requireResolveFunction,
4344
rootDir,
4445
}: {
4546
filePath: string;
4647
humanOptionName: string;
4748
optionName: string;
4849
prefix: string;
50+
requireResolveFunction: (moduleName: string) => string;
4951
rootDir: Config.Path;
5052
},
5153
): string => {
@@ -59,7 +61,7 @@ const resolveWithPrefix = (
5961
}
6062

6163
try {
62-
return require.resolve(`${prefix}${fileName}`);
64+
return requireResolveFunction(`${prefix}${fileName}`);
6365
} catch {}
6466

6567
module = Resolver.findNodeModule(fileName, {
@@ -71,7 +73,7 @@ const resolveWithPrefix = (
7173
}
7274

7375
try {
74-
return require.resolve(fileName);
76+
return requireResolveFunction(fileName);
7577
} catch {}
7678

7779
throw createValidationError(
@@ -94,15 +96,19 @@ const resolveWithPrefix = (
9496
export const resolveTestEnvironment = ({
9597
rootDir,
9698
testEnvironment: filePath,
99+
// TODO: remove default in Jest 28
100+
requireResolveFunction = require.resolve,
97101
}: {
98102
rootDir: Config.Path;
99103
testEnvironment: string;
104+
requireResolveFunction?: (moduleName: string) => string;
100105
}): string =>
101106
resolveWithPrefix(undefined, {
102107
filePath,
103108
humanOptionName: 'Test environment',
104109
optionName: 'testEnvironment',
105110
prefix: 'jest-environment-',
111+
requireResolveFunction,
106112
rootDir,
107113
});
108114

@@ -116,13 +122,23 @@ export const resolveTestEnvironment = ({
116122
*/
117123
export const resolveWatchPlugin = (
118124
resolver: string | undefined | null,
119-
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
125+
{
126+
filePath,
127+
rootDir,
128+
// TODO: remove default in Jest 28
129+
requireResolveFunction = require.resolve,
130+
}: {
131+
filePath: string;
132+
rootDir: Config.Path;
133+
requireResolveFunction?: (moduleName: string) => string;
134+
},
120135
): string =>
121136
resolveWithPrefix(resolver, {
122137
filePath,
123138
humanOptionName: 'Watch plugin',
124139
optionName: 'watchPlugins',
125140
prefix: 'jest-watch-',
141+
requireResolveFunction,
126142
rootDir,
127143
});
128144

@@ -136,24 +152,44 @@ export const resolveWatchPlugin = (
136152
*/
137153
export const resolveRunner = (
138154
resolver: string | undefined | null,
139-
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
155+
{
156+
filePath,
157+
rootDir,
158+
// TODO: remove default in Jest 28
159+
requireResolveFunction = require.resolve,
160+
}: {
161+
filePath: string;
162+
rootDir: Config.Path;
163+
requireResolveFunction?: (moduleName: string) => string;
164+
},
140165
): string =>
141166
resolveWithPrefix(resolver, {
142167
filePath,
143168
humanOptionName: 'Jest Runner',
144169
optionName: 'runner',
145170
prefix: 'jest-runner-',
171+
requireResolveFunction,
146172
rootDir,
147173
});
148174

149175
export const resolveSequencer = (
150176
resolver: string | undefined | null,
151-
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
177+
{
178+
filePath,
179+
rootDir,
180+
// TODO: remove default in Jest 28
181+
requireResolveFunction = require.resolve,
182+
}: {
183+
filePath: string;
184+
rootDir: Config.Path;
185+
requireResolveFunction?: (moduleName: string) => string;
186+
},
152187
): string =>
153188
resolveWithPrefix(resolver, {
154189
filePath,
155190
humanOptionName: 'Jest Sequencer',
156191
optionName: 'testSequencer',
157192
prefix: 'jest-sequencer-',
193+
requireResolveFunction,
158194
rootDir,
159195
});

packages/jest-runner/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"exit": "^0.1.2",
2626
"graceful-fs": "^4.2.4",
2727
"jest-docblock": "^27.0.1",
28+
"jest-environment-jsdom": "^27.0.3",
29+
"jest-environment-node": "^27.0.3",
2830
"jest-haste-map": "^27.0.2",
2931
"jest-leak-detector": "^27.0.2",
3032
"jest-message-util": "^27.0.2",

packages/jest-runner/src/runTest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ async function runTestInternal(
9898
}
9999
testEnvironment = resolveTestEnvironment({
100100
...config,
101+
requireResolveFunction: require.resolve,
101102
testEnvironment: customEnvironment,
102103
});
103104
}

packages/jest-runner/tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
{"path": "../jest-console"},
99
{"path": "../jest-docblock"},
1010
{"path": "../jest-environment"},
11+
{"path": "../jest-environment-jsdom"},
12+
{"path": "../jest-environment-node"},
1113
{"path": "../jest-haste-map"},
1214
{"path": "../jest-leak-detector"},
1315
{"path": "../jest-message-util"},

yarn.lock

+2
Original file line numberDiff line numberDiff line change
@@ -13733,6 +13733,8 @@ fsevents@^1.2.7:
1373313733
exit: ^0.1.2
1373413734
graceful-fs: ^4.2.4
1373513735
jest-docblock: ^27.0.1
13736+
jest-environment-jsdom: ^27.0.3
13737+
jest-environment-node: ^27.0.3
1373613738
jest-haste-map: ^27.0.2
1373713739
jest-jasmine2: ^27.0.3
1373813740
jest-leak-detector: ^27.0.2

0 commit comments

Comments
 (0)