jest
jest copied to clipboard
[Bug]: require.resolve() with `options.paths` ignores `moduleNameMapper`
Version
29.7.0
Steps to reproduce
- Open https://stackblitz.com/edit/stackblitz-starters-7snujh
- Run
jestin a terminal - Notice failing tests
Expected behavior
Tests are simple and assert require.resolve() behavior:
it('require-resolve', () => {
const result = require.resolve('@foo/js');
expect(typeof result).toBe('string');
expect(result).toMatch(/\/js\/index.js$/);
});
it('require-resolve-paths', () => {
const paths = [__dirname];
const result = require.resolve('@foo/js', { paths });
expect(typeof result).toBe('string');
expect(result).toMatch(/\/src\/\@foo\/js\/index.js$/);
});
The expectation is that both tests will pass.
Actual behavior
require-resolve-paths test is failing with:
Message:
Cannot resolve module '@foo/js' from paths ['/home/projects/stackblitz-starters-7snujh/packages'] from /home/projects/stackblitz-starters-7snujh/packages/require-resolve-paths.test.js
While moduleNameMapper is configured:
module.exports = {
moduleNameMapper: {
'@foo/js': '<rootDir>/js/index.js',
},
}
It fails because once options.paths are used, completely different branch is used:
https://github.com/jestjs/jest/blob/1bacb5e0075e1a145d822e8a5dd22ab8aef32c25/packages/jest-runtime/src/index.ts#L1458-L1462
resolveModuleFromDirIfExists() does not check for moduleNameMapper config unlike _resolveCjsModule() (or actually resolveModule() that is used in a default scenario.
Additional context
No response
Environment
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Binaries:
Node: 18.18.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 9.4.2 - /usr/local/bin/npm
pnpm: 8.6.12 - /usr/local/bin/pnpm
npmPackages:
jest: ^29.7.0 => 29.7.0
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Please unstale, bot.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Please unstale, bot.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Please unstale, bot.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Please unstale, bot.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Please unstale, bot.
Wanna send a PR fixing this? I've never really used paths myself...