How vitest resolves dependency exports #4233
-
Hi! so initially I had this issue:
(original question here: https://stackoverflow.com/questions/77223992/vitest-loads-cjs-export-of-dependency-instead-of-esm) After a nice discussion on the Vite discord, I learnt about the From my testing I can confirm that:
Vitest will completely ignore the
or even this
Vitest will go for the Is this described behaviour documented somewhere that I missed? Should this be documented? (I can open an issue and do that) Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Vitest has to use Node.js resolution so you don't have multiple instances of the same package, and Node.js doesn't support
This is not true.
This is just a standard resolution: https://nodejs.org/api/esm.html#resolution-and-loading-algorithm |
Beta Was this translation helpful? Give feedback.
-
Okay, after 2 days I land on this which finally gives me some sanity back. Given that this discussion is the closest thing to explain what's going on, would be wonderful if we could have a reply here or a link on what to do next. The scenario is:
What can I do in code that I own (my tests configuration?, my test file itself?, my component?) to force vitest to load the ESM version of the library the same way that the browser is doing? |
Beta Was this translation helpful? Give feedback.
-
I managed to make the following workaround for this issue. On
These are some example on forcing the module resolution to point to the ESM version instead of the CJS. |
Beta Was this translation helpful? Give feedback.
-
Hi - I'm a library author (DataTables.net) and I've just encountered this due to a support question whereby the developers site was working fine in the browser, but failing in a UMD in vitest. DataTables defines both a So I'm confused as what I should do as a library author here. It seems to me that if a library defines How what Vitest be configured to test the code the client-side will see in such a case? |
Beta Was this translation helpful? Give feedback.
-
For lib authors, I managed to solve this problem by adding
I was tracking this issue on unjs/unbuild#444 |
Beta Was this translation helpful? Give feedback.
Vitest has to use Node.js resolution so you don't have multiple instances of the same package, and Node.js doesn't support
module
field.This is not true.
import
key is chosen when the consumer usesimport
to import the module.This is just a standard resolution: https://nodejs.org/api/esm.html#resolution-and-loading-algorithm