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

jest-resolve can't handle "exports" #10422

Closed
wojtekmaj opened this issue Aug 18, 2020 · 5 comments
Closed

jest-resolve can't handle "exports" #10422

wojtekmaj opened this issue Aug 18, 2020 · 5 comments

Comments

@wojtekmaj
Copy link
Contributor

wojtekmaj commented Aug 18, 2020

🐛 Bug Report

"exports" field seems to be ignored by jest-resolve, resulting with unexpected "Cannot find module ..." error thrown even though Node is capable of resolving the paths.

https://nodejs.org/api/esm.html#esm_package_entry_points

To Reproduce

Steps to reproduce the behavior:

I have a simple utils package in my workspace, that's shaped like this:

/dist
  array.js
  … 
/src
  array.js
  …
package.json

package.json:

{
  "name": "@my-org/utils",
  "version": "1.0.0",
  "exports": {
    "./array": "./dist/array.js",
    "./boolean": "./dist/boolean.js",
    "./math": "./dist/math.js",
    "./number": "./dist/number.js",
    "./object": "./dist/object.js",
    "./promise": "./dist/promise.js",
    "./string": "./dist/string.js"
  }
}

This allows me to use it in my main project by importing utils like this:

import { min } from '@my-org/utils/math'

This works both in native ES modules environment in Node 14, and in code transpiled by Babel. But this code crashes in Jest tests.

Expected behavior

"exports" to be respected and understood by jest-resolve

Link to repl or repo (highly encouraged)

https://github.com/wojtekmaj/resolve-exports-issue

envinfo

  System:
    OS: macOS 11.0
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Binaries:
    Node: 14.5.0 - /usr/local/bin/node
    Yarn: 2.1.1 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.4.0 => 26.4.0 
@akauppi
Copy link

akauppi commented Aug 19, 2020

Facing the same with firebase-jest-testing library. Unable to use it in the application.

@digimbyte
Copy link

I agree, Firebase with full Jest support would be desirable. Any current work arounds for exports?

@SimenB
Copy link
Member

SimenB commented Aug 20, 2020

Duplicate of #9771. I haven't had time to work on ESM support in general for the last few months, and the immediate future doesn't look any more promising in that regard, unfortunately... Any help via PRs or research is of course welcome.

@SimenB SimenB closed this as completed Aug 20, 2020
@guilleflmpeya
Copy link

guilleflmpeya commented Apr 30, 2021

I found this post after the same happened to me.
I´m using
node 12.22.1
Client project dependencies

"devDependencies": {
    "@babel/cli": "^7.13.14",
    "@babel/core": "^7.13.15",
    "@babel/node": "^7.13.13",
    "@babel/preset-env": "^7.13.15",
    "@types/jest": "^26.0.22",
    "babel-jest": "^26.6.3",
    "babel-loader": "^8.2.2",
    "circular-dependency-plugin": "^5.2.2",
    "enhanced-resolve": "^5.8.0",
    "jest": "^26.6.3",
    "jest-sonar-reporter": "^2.0.0",
    "npm-run-all": "^4.1.5",
    "sonarqube-scanner": "^2.8.0",
    "webpack": "^5.33.1",
    "webpack-bundle-analyzer": "^4.4.1",
    "webpack-cli": "^4.6.0"
  }

exporter lib config

"main": "./lib/main.js",
  "exports": {
    ".": "./lib/main.js",
    "./*": "./lib/*.js"
  },
  "files": [
    "lib"
  ],

I found an easy way to implemente a workaround. Hope it can help someone until Jest add support for this case.
In my case, inside my package, I had globalHeaders.js with some named exports. So I was importing like this.

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/globalHeaders";

but I got, can't find module error. Since Jest does not supports
"exports": {
".": "./lib/main.js",
"./": "./lib/.js"
},

I try change my import to

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/lib/globalHeaders";

It worked, but still far from elegant, so I also added this to my Jest config file

moduleNameMapper: {
    "^@domain/package-name/(.*)$": "@appscore/package-name/lib/$1",
  },

And now I can do this as it supposed to be from start

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/globalHeaders";

@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 May 31, 2021
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

5 participants