Skip to content

Commit

Permalink
fix: resolve bundled modules from correct location (#11493)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jun 3, 2021
1 parent bba34bd commit d6fb0d8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,12 @@

### Performance

## 27.0.4

### Fixes

- `[jest-config, jest-resolve]` Pass in `require.resolve` to resolvers to resolve from correct base ([#11493](https://github.com/facebook/jest/pull/11493))

## 27.0.3

### Fixes
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -607,6 +607,7 @@ export default async function normalize(
}

options.testEnvironment = resolveTestEnvironment({
requireResolveFunction: require.resolve,
rootDir: options.rootDir,
testEnvironment:
options.testEnvironment ||
Expand Down Expand Up @@ -745,6 +746,7 @@ export default async function normalize(
option &&
resolveRunner(newOptions.resolver, {
filePath: option,
requireResolveFunction: require.resolve,
rootDir: options.rootDir,
});
}
Expand Down Expand Up @@ -1016,6 +1018,7 @@ export default async function normalize(
config: {},
path: resolveWatchPlugin(newOptions.resolver, {
filePath: watchPlugin,
requireResolveFunction: require.resolve,
rootDir: options.rootDir,
}),
};
Expand All @@ -1024,6 +1027,7 @@ export default async function normalize(
config: watchPlugin[1] || {},
path: resolveWatchPlugin(newOptions.resolver, {
filePath: watchPlugin[0],
requireResolveFunction: require.resolve,
rootDir: options.rootDir,
}),
};
Expand Down Expand Up @@ -1058,6 +1062,7 @@ export default async function normalize(
newOptions.testSequencer = resolveSequencer(newOptions.resolver, {
filePath:
options.testSequencer || require.resolve(DEFAULT_CONFIG.testSequencer),
requireResolveFunction: require.resolve,
rootDir: options.rootDir,
});

Expand Down
46 changes: 41 additions & 5 deletions packages/jest-resolve/src/utils.ts
Expand Up @@ -40,12 +40,14 @@ const resolveWithPrefix = (
humanOptionName,
optionName,
prefix,
requireResolveFunction,
rootDir,
}: {
filePath: string;
humanOptionName: string;
optionName: string;
prefix: string;
requireResolveFunction: (moduleName: string) => string;
rootDir: Config.Path;
},
): string => {
Expand All @@ -59,7 +61,7 @@ const resolveWithPrefix = (
}

try {
return require.resolve(`${prefix}${fileName}`);
return requireResolveFunction(`${prefix}${fileName}`);
} catch {}

module = Resolver.findNodeModule(fileName, {
Expand All @@ -71,7 +73,7 @@ const resolveWithPrefix = (
}

try {
return require.resolve(fileName);
return requireResolveFunction(fileName);
} catch {}

throw createValidationError(
Expand All @@ -94,15 +96,19 @@ const resolveWithPrefix = (
export const resolveTestEnvironment = ({
rootDir,
testEnvironment: filePath,
// TODO: remove default in Jest 28
requireResolveFunction = require.resolve,
}: {
rootDir: Config.Path;
testEnvironment: string;
requireResolveFunction?: (moduleName: string) => string;
}): string =>
resolveWithPrefix(undefined, {
filePath,
humanOptionName: 'Test environment',
optionName: 'testEnvironment',
prefix: 'jest-environment-',
requireResolveFunction,
rootDir,
});

Expand All @@ -116,13 +122,23 @@ export const resolveTestEnvironment = ({
*/
export const resolveWatchPlugin = (
resolver: string | undefined | null,
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
{
filePath,
rootDir,
// TODO: remove default in Jest 28
requireResolveFunction = require.resolve,
}: {
filePath: string;
rootDir: Config.Path;
requireResolveFunction?: (moduleName: string) => string;
},
): string =>
resolveWithPrefix(resolver, {
filePath,
humanOptionName: 'Watch plugin',
optionName: 'watchPlugins',
prefix: 'jest-watch-',
requireResolveFunction,
rootDir,
});

Expand All @@ -136,24 +152,44 @@ export const resolveWatchPlugin = (
*/
export const resolveRunner = (
resolver: string | undefined | null,
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
{
filePath,
rootDir,
// TODO: remove default in Jest 28
requireResolveFunction = require.resolve,
}: {
filePath: string;
rootDir: Config.Path;
requireResolveFunction?: (moduleName: string) => string;
},
): string =>
resolveWithPrefix(resolver, {
filePath,
humanOptionName: 'Jest Runner',
optionName: 'runner',
prefix: 'jest-runner-',
requireResolveFunction,
rootDir,
});

export const resolveSequencer = (
resolver: string | undefined | null,
{filePath, rootDir}: {filePath: string; rootDir: Config.Path},
{
filePath,
rootDir,
// TODO: remove default in Jest 28
requireResolveFunction = require.resolve,
}: {
filePath: string;
rootDir: Config.Path;
requireResolveFunction?: (moduleName: string) => string;
},
): string =>
resolveWithPrefix(resolver, {
filePath,
humanOptionName: 'Jest Sequencer',
optionName: 'testSequencer',
prefix: 'jest-sequencer-',
requireResolveFunction,
rootDir,
});
2 changes: 2 additions & 0 deletions packages/jest-runner/package.json
Expand Up @@ -25,6 +25,8 @@
"exit": "^0.1.2",
"graceful-fs": "^4.2.4",
"jest-docblock": "^27.0.1",
"jest-environment-jsdom": "^27.0.3",
"jest-environment-node": "^27.0.3",
"jest-haste-map": "^27.0.2",
"jest-leak-detector": "^27.0.2",
"jest-message-util": "^27.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -98,6 +98,7 @@ async function runTestInternal(
}
testEnvironment = resolveTestEnvironment({
...config,
requireResolveFunction: require.resolve,
testEnvironment: customEnvironment,
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-runner/tsconfig.json
Expand Up @@ -8,6 +8,8 @@
{"path": "../jest-console"},
{"path": "../jest-docblock"},
{"path": "../jest-environment"},
{"path": "../jest-environment-jsdom"},
{"path": "../jest-environment-node"},
{"path": "../jest-haste-map"},
{"path": "../jest-leak-detector"},
{"path": "../jest-message-util"},
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Expand Up @@ -13733,6 +13733,8 @@ fsevents@^1.2.7:
exit: ^0.1.2
graceful-fs: ^4.2.4
jest-docblock: ^27.0.1
jest-environment-jsdom: ^27.0.3
jest-environment-node: ^27.0.3
jest-haste-map: ^27.0.2
jest-jasmine2: ^27.0.3
jest-leak-detector: ^27.0.2
Expand Down

0 comments on commit d6fb0d8

Please sign in to comment.