jestjs/jest

[Bug]: Jest needs additional config for supporting modules that use subpath imports

ottokruse opened this issue · 3 comments

Version

27.5.1

Steps to reproduce

Repo with reproducable case: https://github.com/ottokruse/jest-subpath-import

  1. Clone my repo: git clone https://github.com/ottokruse/jest-subpath-import.git
  2. Install dependencies: npm install
  3. Run jest: npx jest

Expected behavior

Jest works fine, doesn't throw errors. Under the hood, jest thus understand subpath imports natively, as NodeJS does too. Subpath imports (link) were added to NodeJS in: v14.6.0, v12.19.0

Actual behavior

Jest errors with this message:

 FAIL  ./index.test.js
  ● Test suite failed to run

    Cannot find module '#node-web-compat' from 'node_modules/aws-jwt-verify/dist/cjs/https.js'

    Require stack:
      node_modules/aws-jwt-verify/dist/cjs/https.js
      node_modules/aws-jwt-verify/dist/cjs/jwk.js
      node_modules/aws-jwt-verify/dist/cjs/jwt-rsa.js
      node_modules/aws-jwt-verify/dist/cjs/index.js
      index.test.js

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.283 s
Ran all test suites.

Additional context

Subpath imports (link) were added to NodeJS in: v14.6.0, v12.19.0

However Jest does not support modules using subpath imports, unless jest users do additional config (e.g. specify a moduleNameMapper in their config).

This is not obvious and requires users to waste time googling around looking for a fix.
The "fix", to add a moduleNameMapper in their config, is not a real fix, because it requires the developer to get to know, and use, the implementation details of the module with the subpath imports --> the user needs to copy paste the subpath import definition from the module's package.json into their jest config. This is of course also brittle:

module.exports = {
  // To know what to put here I have to look inside the package.json of the module that has the
  // subpath import, and put the declaration here:
  moduleNameMapper: {
    "#subpath-import-name": "mapping-to-actual-js-file-or-external-module",
  },
};

Environment

System:
    OS: macOS 11.6.5
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
  Binaries:
    Node: 16.8.0 - ~/.nvm/versions/node/v16.8.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.8.0/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v16.8.0/bin/npm
  npmPackages:
    jest: ^27.5.1 => 27.5.1

I hit this snag today

Duplicate of #12270

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.