-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Vitest doesn't handle package.json exports
conditions like browser
#2603
Comments
cc @talkor |
Browser condition usually has ESM that Node doesn't support (like imports without You can enable browser condition (or any condition), using config: export default {
resolve: {
conditions: ['browser']
}
} Vitest passes these conditions to Node with |
Is this in the docs? This whole ESM, CJS, Browser, Node stuff is truly awful... |
This is part of Vite configuration, Vitest doesn't duplicate it, but mentions it:
It truly is |
I ran into this issue and created a stackblitz for repro: https://stackblitz.com/edit/vitest-dev-vitest-vhpesz that uses browser mode but doesn't load browser modules. I put resolve: {
conditions: ['browser']
} in my vite.config.ts but it didn't help. If someone can provide a fixed version of this stackblitz, that would be very helpful. CC: @sheremet-va |
|
I've used:
and had the same result. Removing |
If I have tests using both a node environment and a jsdom environment, configuring |
You can use Vitest workspace for this. |
FYI manually telling node to use the |
Using browser condition will break most application tests. It’s usually not meant to run in Node.js. |
@sheremet-va right, Ideally you'd be able to configure this for specific envs (eg in a workspace). |
Also FWIW in jest that condition is loaded by default for browser envs so I'm curious why not enable that (within the test thread)? You say it would break most tests, but again this is kind of expected since the real app running (usually via bundler, but also via jest) will load the |
Because it cases a ton of problems and literally breaks your tests (lookup jest issues). It is probably one of the worst decisions jest team made. browser condition may be UMD build, it can be unprocessed ESM - it can be anything. Vitest prioritizes working code and performance over semantics. |
You can also always pass down conditions to resolve.conditions. Vitest runs workers with —condition flag. I already mentioned it in this thread. |
There are a large number of isomorphic modules which expect to use different apis in a browser environment which is why every bundler supports this (and every other browser oriented test runner I've used). Having said that, I did try the |
Honestly, this whole ESM vs CJS vs Browser vs Node.js is just a nightmare to deal with, with some packages getting it wrong, and different bundlers, runtimes, and tools, all implementing some parts, and missing others, that I way too often need workarounds to get stuff working, for example: vitejs/vite#9731, microsoft/playwright#23662. Most of this stem from Node.js strict ESM support and the outright refusal of some tools to transpile code to work with it, without having to change it, e.g. TypeScript refusing to automatically add file extensions to imports. JSON imports requiring import assertions, but tools don't add those and some don't even support them in the code (e.g. in Vue SFC). And on and on. And don't get me started on problem packages that require workers, WASM, non-static dynamic imports, static assets and so on, as those don't even have a proper standard on how to package them up for any bundlers, leading to often needing package specific bundler plugins, at the final built application artifact, so you need to manually copy paste those plugin definitions to every resulting final app build you have that transitively uses such a package. |
Since we are using an opinionated tool (Vite) under the hood, we can make certain assumptions about the code we are running, so I am planning to explore ways to make it easier to load those dependencies in Node using VM API (it looks like the Node.js team is working on it in the latest Node versions, nodejs/node#49932 doesn't fix the issue but it's a start) - there is already an option to transform those files in Alternatively, loaders API is already available but it's too unstable (every minor Node.js release breaks it). But even then I don't think we will allow |
All I will say is that it seems a bit strange given that the behavior of Vite itself is to add the browser condition (and browser field). Fine to be opinionated though, and at least I have a work around 👍 |
Describe the bug
We have a package that has a browser ESM & Node CJS & ESM builds. So exports looks something like this:
Vitest selects the Node ESM build even with
environment: "jsdom"
. Annoyingly, this can be a good thing for some packages, and bad for others in tests.See https://jestjs.io/docs/29.0/configuration#testenvironmentoptions-object for the config and special comment syntax for this issue supported by Jest.
This might be difficult to support without loader hooks though Node does have a
--conditions
flag.Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-4jwegg?file=test/basic.test.ts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: