vite-plugin-svelte: test error when using vitePreprocess with vitest@2 and vite@6 - "Cannot create proxy with a non-object as target or handler"
Describe the bug
When using Vite 6 with vite-plugin-svelte, components containing <style> tags (even empty ones) fail during preprocessing in test environments. This appears to be a regression that affects the CSS preprocessing pipeline.
Steps to Reproduce
- Create a new SvelteKit project or use an existing one
- Upgrade to Vite 6
- Create a test file that imports a Svelte component containing a
<style>tag - Run the tests using Vitest
Minimal Example
<!-- TestComponent.svelte -->
<script lang="ts">
let text = 'Hello';
</script>
<div>{text}</div>
<style>
/* Even an empty style tag triggers the error */
</style>
// test.ts
import TestComponent from './TestComponent.svelte';
test('component test', () => {
// Any test that causes the component to be processed
});
Error Message
TypeError: Error while preprocessing [component path] - Cannot create proxy with a non-object as target or handler
at new PartialEnvironment (.../vite/dist/node/chunks/dep-A4nAWF7x.js:16766:19)
at preprocessCSS (.../vite/dist/node/chunks/dep-A4nAWF7x.js:48446:23)
at .../vite-plugin-svelte/src/preprocess.js:114:10
Environment
- Vite: 6.0.2
- @sveltejs/vite-plugin-svelte: 5.0.1
- Svelte: 5.1.2
- Node: 22.10.1
Additional Context
- The issue appears to be specific to the test environment
- Production builds and development mode seem unaffected
- Removing the
<style>tag from components allows tests to pass - This was discovered while working with sveltekit-superforms, but the issue appears to be more general to the plugin’s CSS preprocessing
Workaround
Currently, the only known workaround is to remove <style> tags from components being tested, which is not a viable long-term solution.
Would you like me to modify any part of this issue before you submit it?
Reproduction URL
https://stackblitz.com/edit/vitejs-vite-7gxfjeuj?file=src%2Fapp.html
Reproduction
No response
Logs
No response
System Info
System:
OS: macOS 15.1.1
CPU: (8) arm64 Apple M2
Memory: 78.66 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 23.3.0 - ~/.local/share/mise/installs/node/23.3.0/bin/node
npm: 10.9.0 - ~/.local/share/mise/installs/node/23.3.0/bin/npm
pnpm: 9.14.2 - ~/.local/share/mise/installs/pnpm/9.14.2/bin/pnpm
Browsers:
Chrome: 128.0.6613.115
Safari: 18.1.1
npmPackages:
@sveltejs/vite-plugin-svelte: ^5.0.1 => 5.0.1
svelte: ^5.9.0 => 5.9.0
vite: ^6.0.3 => 6.0.3
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 16 (10 by maintainers)
not sure what the best recommendation here is. Both overriding vite for vitest@2 and moving to vitest@3 beta contains some risk of it leading to unexpected results. Depending on your own assesment you want to pick either of these options or stick to vite5 until vitest@3 is released (Which can take a few weeks)
https://github.com/vitest-dev/vitest/releases/tag/v3.0.0-beta.1
this is caused by vitest@2 (with the exception of 2.1.6 which was rolled back) not supporting vite@6 internally. You either have to update to vitest-3.0.0-beta or add an override to your package manager config (“resolutions” in package.json to ensure vitest@2 uses vite6 instead of vite5.