-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Feature]: Support package imports in jest-resolve #12270
Comments
Is there a current work around for the imports feature in jest? |
No, but once we have |
I think this is not only a feature, but a bug. Projects using some popular packages, such as An example is #12309. |
If anyone is looking for a workaround for now: "moduleNameMapper": {
"chalk": "chalk/source/index.js",
"#ansi-styles": "chalk/source/vendor/ansi-styles/index.js",
"#supports-color": "chalk/source/vendor/supports-color/index.js"
}, https://github.com/qiwi/libdefkit/blob/master/jest.config.json#L22 |
Once a module implementing the algorithm exists (e.g. lukeed/resolve.exports#14) we should be good to go for adding support here. |
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. |
I've been wrestling with this problem today. I thought I could address it by adding a resolver that uses |
Plugging in a resolver should work fine, but it needs to support all cases. You could choose to e.g. look for |
moduleNameMapper: {
chalk: require.resolve("chalk/source/index.js"),
"#ansi-styles": require.resolve("ansi-styles/index.js"),
"#supports-color": require.resolve("supports-color/index.js"),
}, This solution works for me: moduleNameMapper: {
chalk: require.resolve("chalk"),
"#ansi-styles": path.join(
require.resolve("chalk").split("chalk")[0],
"chalk/source/vendor/ansi-styles/index.js",
),
"#supports-color": path.join(
require.resolve("chalk").split("chalk")[0],
"chalk/source/vendor/supports-color/index.js",
),
}, |
the aws-jwt-verify module currently needs to be set to 2.1.3 vs. 3.x due to a awslabs/aws-jwt-verify#66 in how it jestjs/jest#12270. Until this is fixed upstream, avoid using the latest version.
#12270 (comment) does not work in applications where chalk v4 (without So I use the workaround of using jest's default resolver whenever possible. This also works for applications with both chalk v4 and chalk v5. // jest.config.mjs
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const dir = join(dirname(fileURLToPath(import.meta.url)));
const jestConfig = {
// ...
resolver: join(dir, 'src/test-util/jest/resolver.cjs'),
};
export default jestConfig; // src/test-util/jest/resolver.cjs
// @ts-check
const { dirname, resolve } = require('path');
const resolveFrom = require('resolve-from');
/**
* @typedef {{
* basedir: string;
* conditions?: Array<string>;
* defaultResolver: (path: string, options: ResolverOptions) => string;
* extensions?: Array<string>;
* moduleDirectory?: Array<string>;
* paths?: Array<string>;
* packageFilter?: (pkg: any, file: string, dir: string) => any;
* pathFilter?: (pkg: any, path: string, relativePath: string) => string;
* rootDir?: string;
* }} ResolverOptions
* */
/** @type {(path: string, options: ResolverOptions) => string} */
module.exports = (path, options) => {
// workaround for https://github.com/facebook/jest/issues/12270
if (path === '#ansi-styles' || path === '#supports-color') {
const chalkRoot = resolve(dirname(resolveFrom(options.basedir, 'chalk')), '../');
const subPkgName = path.slice(1);
return `${chalkRoot}/source/vendor/${subPkgName}/index.js`;
}
return options.defaultResolver(path, options);
}; |
import resolve from 'enhanced-resolve'
import { SyncResolver } from 'jest-resolve'
const resolver: SyncResolver = (path, options) => {
const { defaultResolver } = options
try {
return defaultResolver(path, options)
} catch (e) {
const result = resolve.sync(options.basedir, path)
if (result) {
return result
} else {
throw e
}
}
}
export default resolver |
As more options to solve this pile up here, I'd like to point out that using Jest to test code that has dependencies that have package imports (a.k.a subpath imports) should ideally just work, without doing any config or setting up resolvers. Concur with @KermanX that this is not a feature request, but a bug. Are FB engineers working on Jest, or are we solely waiting on a PR from the community? |
Fixed it for me by just switching from the default runner to https://github.com/nicolo-ribaudo/jest-light-runner, which disables most of Jest's “magic” features I don't need anyways. |
I couldn't figure out how to use @BlackGlory script, so I did this:
const resolve = require("enhanced-resolve");
const resolver = (path, options) => {
const { defaultResolver } = options;
try {
return defaultResolver(path, options);
} catch (e) {
const result = resolve.sync(options.basedir, path);
if (result) {
return result;
} else {
throw e;
}
}
};
module.exports = resolver;
export default {
preset: "ts-jest/presets/default-esm",
globals: {
"ts-jest": {
useESM: true,
},
},
resolver: "./resolver.cjs",
}; |
To add to #12270 (comment) and #12270 (comment), here's an alternative implementation of the custom resolver which relies on the const nativeModule = require('node:module');
function resolver(module, options) {
const { basedir, defaultResolver } = options;
try {
return defaultResolver(module, options);
} catch (error) {
return nativeModule.createRequire(basedir).resolve(module);
}
}
module.exports = resolver; Note: Since Presumably, at some point when Jest starts importing import url from 'node:url';
export default async function resolver(path, options) {
const { basedir, defaultResolver } = options;
try {
return defaultResolver(path, options);
} catch (error) {
return await import.meta.resolve(path, url.pathToFileURL(basedir));
}
} This relies on |
I have previously released a package I will be happy if this is of any help. |
If helpful, until this issue is closed, the following works for the sole case of resolving
|
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Node.js =12.20.0 added support for exports field. Node.js =12.19.0 added support for imports field. Both of these fields are used by integrated ApiDOM. Jest@29.3.0 doesn't yet support package.json imports field. Next version will, more info in: jestjs/jest#12270. Refs #2744
Can confirm this is working now, thank you! I can remove my |
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. |
🚀 Feature Proposal
Node Imports in packages.json
Motivation
https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#imports
Example
No response
Pitch
similar as #9771
The text was updated successfully, but these errors were encountered: