-
-
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
feat: pass conditions
when resolving modules
#11859
Conversation
packages/jest-runtime/src/index.ts
Outdated
@@ -173,6 +173,9 @@ const supportsNodeColonModulePrefixInImport = (() => { | |||
return stdout === 'true'; | |||
})(); | |||
|
|||
const esmConditions = ['import', 'require', 'default']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node.js doesn't pass require
for ESM: https://nodejs.org/api/packages.html#packages_conditional_exports ("import"
and "require"
are mutually exclusive).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though import
supports both? I guess the idea is to fallback to default
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just node
I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it fallbacks to either default
or node
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should i remove it or pass node
you think? (Still somewhat worried about that browser
thing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it for now, let's see how that works out 🙂 Better to have too little than too much
This bugged me too much, so I neglected work today to work on this 😅 Managed to fix the caching issue, so this just needs docs now |
const moduleID = this._resolver.getModuleID( | ||
this._virtualMocks, | ||
from, | ||
moduleName, | ||
isInternal ? undefined : {conditions: cjsConditions}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is wrong and just to avoid the fact pathFilter
doesn't really work for what we want (it asks for every relative import within a package as well). Should probably rework the test, but since this is internal loading it doesn't really matter up until we actually support conditions
out of the box.
I do think we shouldn't use user-provided resolvers when loading our own stuff, but it is what it is
packages/jest-runtime/src/index.ts
Outdated
@@ -294,6 +298,9 @@ export default class Runtime { | |||
const moduleID = this._resolver.getModuleID( | |||
this._virtualMocks, | |||
filePath, | |||
undefined, | |||
// is this correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
automock is weird, and this should be a fully resolved path already, so I guess we can remove it as it doesn't matter
@@ -173,7 +173,7 @@ const supportsNodeColonModulePrefixInImport = (() => { | |||
return stdout === 'true'; | |||
})(); | |||
|
|||
// consider "node" condition as well - maybe switching on `global.window` in the test env? | |||
// consider `node` & `browser` condition as well - maybe switching on `global.window` in the test env? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe they could be enabled by jest-environment-*
packages? I don't know if the internal architecture makes it impossible, but from my (external, user) point of view it's what makes more sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely a possibility! Main worry is browser
meaning either ESM or CJS, but I guess we can add the option and just not use it ourselves for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open up a separate PR with that after landing this 🙂
Codecov Report
@@ Coverage Diff @@
## main #11859 +/- ##
==========================================
- Coverage 68.95% 68.93% -0.02%
==========================================
Files 312 312
Lines 16400 16411 +11
Branches 4750 4759 +9
==========================================
+ Hits 11308 11313 +5
- Misses 5065 5071 +6
Partials 27 27
Continue to review full report at Codecov.
|
This is going to be a bit of a gnarly rebase for #11540. I don't suppose we can hold off on doing more work on the jest-resolver until that one is in, could we? Or, if you don't think that 11540 will land, I'll go ahead and close it out and stop trying to keep it up-to-date. Edit, after looking a little closer, it hopefully won't be too bad to rebase. But it would be nice to not keep spending time rebasing and fixing merge conflicts. ;) |
@IanVS I do have plans to merge that PR, and I sympathise with having to keep it up to date. I don't think there'll be any further changes to the resolver after this PR (unless there are bugs). Adding support for |
Hi, |
I'd assume #9771 (comment) works fine, but passing |
@SimenB I don't know if it's expected, but I'm getting Maybe this is expected, but it would be nice if Jest defaulted to |
@nicolo-ribaudo Hmm, good point. The thing is we resolve those files when normalizing configuration (i.e. way before actually loading them for individual test files, and before loading e.g. We might need to pass whatever path is given to us through in Jest rather that resolving in One thing we could do (if we don't already) is to skip the resolver call if the path is absolute - I don't there's any need for us to resolve again in those cases? |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
A start on implementing support for package exports (#9771). This should unblock people when writing their own resolvers. We'll need to add builtin support in the future, but at least unblocking and letting people write their own and publish is a great start.
It is now correctly passing through aconditions
array, but there is some caching in eitherjest-resolve
orjest-haste-map
(or someplace else...) that returns the wrong module back, meaning CJS tries to load ESM and fails. Running the test files one by one always works, as does running with--no-cache
. Need to investigate, but I'm out of time (& energy) for now.Test plan
Test added