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

Subpath imports are not resolved #11100

Closed
googol opened this issue Feb 21, 2021 · 21 comments
Closed

Subpath imports are not resolved #11100

googol opened this issue Feb 21, 2021 · 21 comments

Comments

@googol
Copy link

googol commented Feb 21, 2021

🐛 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 
@SimenB
Copy link
Member

SimenB commented Feb 21, 2021

@ljharb will this come at the same time as exports in resolve, or is it separate?

@ljharb
Copy link
Contributor

ljharb commented Feb 21, 2021

Hopefully, but it’s a bit unclear since subpath imports only work from inside a package context.

@jakobrosenberg
Copy link

Are subpath imports supported in Jest 27?

@SimenB
Copy link
Member

SimenB commented May 11, 2021

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

@zdm
Copy link

zdm commented May 14, 2021

We are using imports in all our packages, so it is currently impossible to integrate with jest.

@jakobrosenberg
Copy link

jakobrosenberg commented May 15, 2021

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

@googol
Copy link
Author

googol commented May 15, 2021

The is the moduleNameWrapper you mean, right?

@jakobrosenberg
Copy link

@googol that is correct.

@zdm
Copy link

zdm commented May 16, 2021

Do you know, how fast native jest support for imports can be implemented?

@stefcameron
Copy link

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?

@cborchert
Copy link

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

@cborchert
Copy link

@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 !

@stefcameron
Copy link

@cborchert Bingo! Thank you. 😄 I guess that's the one mapping combination I didn't try.

@cborchert
Copy link

Glad it worked out :)

@TheAlexLichter
Copy link

@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.

@ljharb
Copy link
Contributor

ljharb commented Jan 3, 2022

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).

Jaid added a commit to Jaid/eslint-config-jaid that referenced this issue Jan 4, 2022
@stefcameron
Copy link

Following-up after the release of Jest 28, I was able to drop the custom '@eslint/eslintrc': '@eslint/eslintrc/dist/eslintrc-universal.cjs', mapping. Having it there was actually now causing my custom ESLint rule tests to fail (only those, though, not other tests). But I was not able to remove other mappings I had to other modules using subpath exports (they being from one package to another in the same monorepo, in case that matters for some reason).

@SimenB
Copy link
Member

SimenB commented Apr 25, 2022

imports are not yet supported unfortunately. #12270 (comment) is essentially latest, and I actually think that issue can be tracked instead (even though it's newer, its info is more relevant)

@SimenB SimenB closed this as completed Apr 25, 2022
@milesj
Copy link

milesj commented May 12, 2022

I seem to be in a weird state. With Jest 28 I get:

    Configuration error:

    Could not locate module @eslint/eslintrc mapped as:
    @eslint/eslintrc/dist/eslintrc-universal.cjs.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/@eslint\/eslintrc/": "@eslint/eslintrc/dist/eslintrc-universal.cjs"
      },
      "resolver": undefined
    }

But if I remove the mapping, I'm back to the original error.

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

@stefcameron Was there any other changes you made by chance?

@stefcameron
Copy link

@milesj

@stefcameron Was there any other changes you made by chance?

I wish there was more than this, but this really is the only change I made as far as the Jest options go:
image

Aside from that, I had to add a dependency to "jest-environment-jsdom": "^28.0.0" and call jest.useRealTimers(); in a few additional places where it looks like I had forgotten to explicitly restore timers after enabling fake timers.

That was the extent of the changes that got me going after upgrading to Jest 28. I was on 27.5.1 before that.

I do recall seeing a strange error related to this ESLint module after upgrading, and then I just tried removing the mapping since I had a feeling subpath support might get added to Jest 28, and it worked. I was on "eslint": "^8.14.0" at the time I upgraded Jest.

@github-actions
Copy link

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 Jun 12, 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

9 participants