jest: Subpath imports are not resolved

šŸ› Bug Report

When running jest for a project that uses subpath imports, jest fails to resolve the imports.

To Reproduce

Steps to reproduce the behavior:

  • add a subpath import definition to the package.json (#logger mapping to ./logger/default.js in my repro)
  • import that subpath in the code used by the jest tests
  • run the tests

Expected behavior

I would expect the import to be resolved just like when running the code itself, and the tests to run.

Link to repl or repo (highly encouraged)

https://github.com/googol/jest-subpath-import-repro

envinfo

  System:
    OS: Linux 5.10 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 15.8.0 - /usr/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 6.14.11 - /usr/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@stefcameron What worked for us was mapping the eslint-universal.cjs directly to the import name of @eslint/eslintrc. Like this:

"moduleNameMapper": {
    "@eslint/eslintrc": "@eslint/eslintrc/dist/eslintrc-universal.cjs"
  },

Our error had been

   TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 'http://localhost/eslintrc.cjs'

      at Function.createRequire (node_modules/jest-runtime/build/index.js:1813:23)
      at Object.<anonymous> (node_modules/@eslint/eslintrc/lib/config-array-factory.js:57:17)
      at Object.<anonymous> (node_modules/eslint/lib/cli-engine/cli-engine.js:33:5)

and, looking at node_modules/eslint/lib/cli-engine/cli-engine.js line 33, we have require("@eslint/eslintrc"); and so it was the import of @eslint/eslintrc which needed to be remapped. You seem to have the same problem.

Good luck and please let me know if this worked for you !

I seem to be hitting this issue with ESLint 8.x (8.2.0 right now) and jest 27.3.1. I have local/custom ESLint rules, which I test with Jest and ESLintā€™s RuleTester, but when I try to upgrade from ESLint 7 to 8, those specific tests start to fail with this error:

    TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 'http://localhost/eslintrc.cjs'

      at Function.createRequire (node_modules/jest-runtime/build/index.js:1813:23)
      at Object.createRequire (node_modules/@eslint/eslintrc/lib/config-array-factory.js:57:17)
      at Object.<anonymous> (node_modules/eslint/lib/cli-engine/cli-engine.js:33:5)

@mdjermanovic suggested the following

// ------- jest.config.js -------------------

module.exports = {
    "moduleNameMapper": {
        "@eslint/eslintrc/universal": "@eslint/eslintrc/dist/eslintrc-universal.cjs"
    }
}

But this doesnā€™t help me either. I also tried mapping '@eslint/eslintrc': '@eslint/eslintrc/dist/eslintrc.cjs', since that seems to be the default mapped import in @eslint/eslintrcā€™s package.json:

  "main": "./dist/eslintrc.cjs",
  "exports": {
    ".": {
      "import": "./lib/index.js",
      "require": "./dist/eslintrc.cjs"
    },
    "./package.json": "./package.json",
    "./universal": {
      "import": "./lib/index-universal.js",
      "require": "./dist/eslintrc-universal.cjs"
    }
  },

But still no fix.

Am I at least on the right path here, thinking this is my issue and I need to wait for Jest to support these ā€œsubpath importā€ things (didnā€™t know about such mappings until now)? Or is this some other issue and I should look elsewhere?

More packages by different authors wonā€™t likely follow tho; thereā€™s a reason virtually nobody has upgraded from chalk v4 to chalk v5 (see the download counts per version on npm).

@zdm you can get around that by using moduleNameMapper for now.

Wondering if this would work for sub-dependencies too (e.g. Chalk v5 is using subpath imports as well now and more packages will follow). Seems to be quite a hassle when more packages adapt it.

Nope, waiting for resolve to either add it or decide not to. Either way blocked by resolve supporting exports and imports fields

imports are not yet supported unfortunately. https://github.com/facebook/jest/issues/12270#issuecomment-1034800773 is essentially latest, and I actually think that issue can be tracked instead (even though itā€™s newer, its info is more relevant)

@cborchert Bingo! Thank you. šŸ˜„ I guess thatā€™s the one mapping combination I didnā€™t try.

Following. I have the exact same issue as @stefcameron with testing custom ESLint rules (jest 27.3.1 and eslint 8.2.0)