-
Notifications
You must be signed in to change notification settings - Fork 33
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
Error when testing Svelte + Preact components #381
Comments
Oh no! I can take some time to look into this today, thanks for setting up a repro. Does anything improve if you set the resolveBrowser option of the svelteTesting({
// disable browser resolution condition
resolveBrowser: false,
}) That commonjs stuff in your logs also seems like a bad sign! |
@molily I traced the issue to an apparent error in how Preact creates and specifies its browser bundles: preactjs/preact#4406 As a workaround, I was able to get the tests running with the following Vite config: import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { svelteTesting } from '@testing-library/svelte/vite';
import preact from '@preact/preset-vite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact(), svelte(), svelteTesting()],
test: {
environment: 'jsdom',
deps: {
optimizer: {
web: {
enabled: true,
include: ['@testing-library/preact'],
},
},
},
},
}); |
@molily Preact has indicated that the ticket I filed above is a Unfortunately, for Preact v10, this leaves us with a fundamental incompatibility with how the two projects specify their exports:
I recommend you either use the workaround I listed above or create separate suites for your Preact and Svelte components. Preact v11 may not have this same issue, but I have not tested it and it's not a production release yet |
Thanks a lot for your tremendous efforts – thanks for digging into this, discussing this with the Preact maintainer and explaining everything. I'm sorry for bringing this issue up again and again every six months for the last two years, bothering the maintainers of several projects. 🫣😅 Regarding this solution:
This still seems to cause an error in a Preact component with useState:
What continues to work for me is the tip you gave in #222 (comment), with all the constraints you described:
with
I think this is the safest solution in the long term. 😕 Thanks again! |
That error means you're loading two (or more) copies of Preact at once, when hooks by design (in both React & Preact) require to be loaded as singletons. Vitest seems to struggle with this quite a bit IME. That workaround is probably causing both the There's a fair number of threads regarding this over on the Vitest thread IIRC, though it sounds like you might already have a workaround.
Can't speak for Svelte, but as the maintainer for a fair few of those conversations on the Preact side of things, no need to apologize! It's not a bother at all. I (and most other OSS maintainers) are always happy to help those who are grateful/not rude. Sorry we couldn't figure out a better solution for you though |
@molily thanks for all the feedback on workarounds, I'm definitely keeping an eye on this to look for potential improvements. I use Svelte at work, but Preact is my library of choice for personal projects, so I selfishly would love this to be better, too. If you're sticking with Svelte v4 and you don't mind certain limitations listed in #222, you can set the Svelte testing plugin to @rschristian thanks for your feedback over in preactjs/preact#4406 and taking the time to hop in here, too. Please let me know if you think of anything I could do here - or conversations I could start/continue with the Svelte or Vitest folks - to improve interop here! |
Hello, first of all, thank you for this great project.
I have a Vite project with Preact and Svelte components, both tested with Vitest (vitest, jsdom, @testing-library/svelte, @testing-library/preact).
For some reason, this seemingly simple setup causes problems again and again. :-D
Possible context:
vitest-dev/vitest#737
sveltejs/vite-plugin-svelte#581
The Svelte tests alone work fine, the Preact tests alone also work fine. But when I'm adding
import { svelteTesting } from '@testing-library/svelte/vite'
to the Vite plugins, the Preact tests fail with this error:Steps to reproduce:
plugins: [preact(), svelte(), svelteTesting()],
https://github.com/molily/vitest-preact-svelte/blob/main/vite.config.js
Minimal repo:
https://github.com/molily/vitest-preact-svelte
Clone, npm install, then run
npm run test
I think the last time we had to bring the maintainers of Vitest and Preact together to solve this. I'm happy to open an issue on the Preact side if necessary. I'm wondering if there is anything that can be done on the Svelte Testing Library side.
Thanks a lot for your time.
The text was updated successfully, but these errors were encountered: