-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Importing a file which uses "$app/environment": Cannot read properties of undefined (reading 'SSR') #2298
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
Comments
As you can see by this line in stacktrace:
Importing svelte module was delegated to Node. You can fix it by including But I thought Svelte team is using |
no changes that i'm aware of. sveltekit and vite-plugin-svelte auto-generate vite config, is that retained in vitest? ie does it honor the config returned after all config hooks of plugins are done? Would need a reproduction project and i'd recommend checking with pnpm (to rule out yarn pnp issues) and also without a merged config in vitest.config.ts, instead just add the test key to vite.config.js generated from |
maybe @nickbreaton has an idea about aliases support. |
I think alias is working, since it tries to load file inside node_modules. But I don’t understand why it’s not processed by Vitest 🤔 Isn’t this package inlined with ssr.external? Now that I wrote this, I think the problem might be with how we check the package 🫣 It’s a simple „node_modules/${name}“ check - this is probably why it doesn’t work with externals and yarn. |
vite-plugin-svelte-kit indeed sets ssr.external for @sveltejs/kit https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/vite/index.js#L279 resolved ssr config {
format: 'esm',
target: 'node',
noExternal: [ 'svelte', /^svelte\//, '@testing-library/svelte' ],
external: [ '@testing-library/dom', '@sveltejs/kit' ],
optimizeDeps: { disabled: true, esbuildOptions: { preserveSymlinks: false } }
} using adding The value for |
Vitest delegates importing files from
You can define
Or in config: export default {
test: {
env: {
SSR: ''
}
}
} You can also just reassign it: import.meta.env.SSR = '' All |
related: vitejs/vite#8952 should kit be in noExternal then for vitest? |
also related/same problem: sveltejs/kit#6259 |
Vitest runs code through Vite-compatible runtime only in this situations:
If it's not in one of those, code will just me imported using Node native |
This should be fixed as of |
Describe the bug
I'm trying to get started writing unit tests for my Svelte/SvelteKit app using
vitest
. However a few files use some variables from the "$app/environment" which causes an exception to be thrown invitest
:Reproduction
Create a Svelte
.ts
file, which imports, for example, thebrowser
ordev
constant:Create a
vitest
spec file which imports this file:I've also tried to vi.importMock the "$app/environment" with no luck:
My
vitest.config.ts
file looks like this:And my
vite.config.ts
:System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: