jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: require.resolve() with `options.paths` ignores `moduleNameMapper`

Open layershifter opened this issue 2 years ago • 8 comments
trafficstars

Version

29.7.0

Steps to reproduce

  1. Open https://stackblitz.com/edit/stackblitz-starters-7snujh
  2. Run jest in a terminal
  3. 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

layershifter avatar Oct 04 '23 09:10 layershifter

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.

github-actions[bot] avatar Nov 03 '23 10:11 github-actions[bot]

Please unstale, bot.

layershifter avatar Nov 03 '23 11:11 layershifter

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.

github-actions[bot] avatar Dec 03 '23 13:12 github-actions[bot]

Please unstale, bot.

layershifter avatar Dec 04 '23 19:12 layershifter

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.

github-actions[bot] avatar Jan 03 '24 20:01 github-actions[bot]

Please unstale, bot.

layershifter avatar Jan 03 '24 20:01 layershifter

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.

github-actions[bot] avatar Feb 02 '24 22:02 github-actions[bot]

Please unstale, bot.

layershifter avatar Feb 03 '24 08:02 layershifter

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.

github-actions[bot] avatar Mar 04 '24 09:03 github-actions[bot]

Please unstale, bot.

layershifter avatar Mar 04 '24 10:03 layershifter

Wanna send a PR fixing this? I've never really used paths myself...

SimenB avatar Mar 04 '24 13:03 SimenB