Replies: 2 comments 4 replies
-
I understand this perspective, but Vite and Vitest's import resolutions and transforms are too forgiving compared to actual NodeJS runtime in many aspects, so if you need to test pure NodeJS use case, then Vitest is not technically a good fit (unless Vitest provides such stricter mode #3849). Also, maybe this self-reference issue is simply a Vite bug and it looks like this is going to be fixed soon: |
Beta Was this translation helpful? Give feedback.
-
This has shipped in Vite 5.2.0 and can be used in Vitest today, so I'm closing this discussion. Thanks so much @hi-ogawa for all your help! |
Beta Was this translation helpful? Give feedback.
-
The default way of using Vitest is to just directly import the file you are testing with a relative path, which obviously works.
There are some people (e.g. https://fosstodon.org/@rauschma/112066537954393244, see comments) who would like to test the public API of their package. In Node.js this is possible by just importing whatever you want to test by referencing your package name and your package.json "exports". It would look like this:
@ifiokjr asked about this feature in #1394 but @antfu closed that as "possible with
resolve.alias
". The problem with usingresolve.alias
is that the main use case of self-referencing your package is to test the package through the public API, and if you're manually replicating the behavior of package.json'sexports
field inresolve.alias
you're not really testing the public API anymore.Please reconsider implementing the specced Node.js module resolution in Vitest that allows self-referencing the package via package.json exports.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions