Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ottokruse opened this issue Mar 31, 2022 · 3 comments

Comments

@ottokruse
Copy link

ottokruse commented Mar 31, 2022

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
@bmccann36
Copy link

I hit this snag today

@SimenB
Copy link
Member

SimenB commented Apr 1, 2022

Duplicate of #12270

@SimenB SimenB marked this as a duplicate of #12270 Apr 1, 2022
@SimenB SimenB closed this as completed Apr 1, 2022
@github-actions
Copy link

github-actions bot commented May 2, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants