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 does not correctly resolve wildcards in subpath imports #15250

Closed
JavaScriptBach opened this issue Aug 7, 2024 · 6 comments
Closed

Comments

@JavaScriptBach
Copy link

Version

29.7.0

Steps to reproduce

See https://github.com/JavaScriptBach/jest-repro

Expected behavior

When a subpath import with a wildcard is defined, Jest does not correctly resolve it.

For example, if I define

"imports": {
    "#test/*.css": "./src/*.css",
    "#test/*.ts": "./src/*.ts"
},

Then an import of #test/asdf/css.ts should resolve to ./src/asdf/css.ts.

Actual behavior

Instead, Jest resolves it to ./src/asdf.css.

Additional context

My envinfo below is macOS, but this issue also happens on Linux.

Environment

System:
    OS: macOS 14.6
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.14.0/bin/yarn
    npm: 9.3.1 - ~/.nvm/versions/node/v18.14.0/bin/npm
    pnpm: 9.7.0 - ~/.nvm/versions/node/v18.14.0/bin/pnpm
Copy link

github-actions bot commented Sep 7, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Sep 7, 2024
@JavaScriptBach
Copy link
Author

Bump

@github-actions github-actions bot removed the Stale label Sep 7, 2024
Copy link

github-actions bot commented Oct 7, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@SimenB
Copy link
Member

SimenB commented Oct 7, 2024

This is a bug in resolve.exports, which is what we use to map the imports and exports patterns.

const { imports } = require('resolve.exports');

const resolved = imports({
  'imports': {
    '#test/*.css': './src/*.css',
    '#test/*.ts': './src/*.ts',
  },
}, '#test/asdf/css.ts');

console.log(resolved);

Logs out [ './src/asdf.css' ].

Mind opening up an issue there? If I delete the *.css pattern it works correctly.

@JavaScriptBach
Copy link
Author

Shouldn't the fix be to use require.resolve or import.meta.resolve instead? This way it is using Node.js's module resolution instead of a third-party library's.

@SimenB
Copy link
Member

SimenB commented Oct 14, 2024

No, we cannot configure that to work with e.g. browser resolution or moduleMapper or mocks or anything. So we need a custom implementation.

I've tried many times to get node to expose their resolution algorithm, but they're not particularly interested in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants