-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
"browser" export condition is not set when testing with jsdom #7215
Comments
Hello @rChaoz. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with |
@hi-ogawa hi, just added a stackblitz repro |
Technically you can add conditions in the same way as Vite via However, not doing this for jsdom/happy-dom is probably intentional since they are only emulation on Node and forcing |
I did not know that, but I guess it makes sense, so mocking |
Let me close this as If you need to mock |
Describe the bug
When testing SvelteKit code, various parts can depend on whether running in the browser like so:
where
browser
is defined using esm-env, which does it using conditional exports inpackage.json
:Now,
development
is set, so unlessbrowser
is also set, it will always be false. I know you can set export conditions in Vite, but I haven't seen anything similar in Vitest. I'd expect it to be set when using a browser environment, probably using an option or similar.Reproduction
Stackblitz: https://stackblitz.com/edit/sveltejs-kit-template-default-muke8nsy
Steps: Just wait for install and run
vitest
in consoleWorkaround
I found this way to mock it, it works for me but I believe the property is also used by SvelteKit's vite plugin, which won't see this change, so there's some edge cases in which it might not work:
Interestingly you cannot wrap the entire call in an
if (typeof window == "object")
as the mock somehow leaks into tests for which the check fails, this way it re-mocks it and removes the property for those.The text was updated successfully, but these errors were encountered: