-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
SvelteSet
does not behave correctly in Vitest
#13961
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
Do you have JSDOM set as your Vitest env? |
Yes, it's enabled in the reproduction repo. Also, side note, it's not very clear from the docs that jsdom is needed for effects to work. |
Svelte 5 uses microtasks for scheduling, you can force things to be sync by using item.selected = true;
flushSync(); However, given you're not using effects, I doubt that will change things here. You can try wrapping your logic in an effect (within the |
I did try flushSync but it didn't work. I'm a bit confused though. The code is already wrapped in an effect root. |
Yeah it likely won't make a difference. I'll take a look into this on Monday :) I'm away without my laptop this weekend |
I copied that code into my local SvelteKit project and it just worked. export default defineConfig((cfg) => ({
// ...
test: {
// (added before a change was made that allows `.svelte.` in other positions)
include: ['src/**/*.test.svelte.{js,ts}'],
}
})); |
@brunnerh What if you do this without SvelteKit? i.e default |
|
Did |
Can you clone the reproduction repo and test if it doesn't work for you? That seems very strange... |
I see an issue though: This path won't work AFAIK. The Maybe try: |
No, that was changed (as noted in my comment in my config). Tested the reproduction and it fails for whatever reason.
|
None of this makes sense, unless vite is doing some module isolation logic that means that the runtime and the tests aren't connected. Like I said, I have no computer till Monday :/ |
I think the issue is that vitest is running the server runtime (vitest-dev/vitest#2603). If you set |
I really don't know what the right solution is here. I guess that Svelte needs It's also possible that you would have some luck with browser mode: https://vitest.dev/guide/browser/ |
Hmm. What wasn't this an issue with Svelte 4 though? |
This sums it up well: testing-library/svelte-testing-library#222 (comment) We should add something along those lines to our documentation. The aliasing is a bit hacky, ideally we'd expose it in a robust way, but I'm not sure if there's a good way to do that via vite-plugin-svelte for example (maybe @dominikg has a good idea how to go about it). |
svelte testing library published a utility plugin for this, but if it is needed for unit testing runic code in .svelte.ts files too we should think about adding it to vps. needs options to disable it though bc there can be cases where you don't want the browser condition to apply (testing ssr, dependencies that use browser globals with it) https://testing-library.com/docs/svelte-testing-library/setup#vitest |
I need to open an issue on testing library about this but we should also include it in the docs: if you set resolve codition browser All of this imho is worth adding to the docs, it might be a bit complicated but i spent 4 hrs last day trying to figure out what was happening. |
The issue I referenced goes into detail about this, and proposes using Example: User does import { svelte } from '@sveltejs/vite-plugin-svelte';
// ...
export default defineConfig({
plugins: [svelte({ testing: { browserAlias: true } })] // name open to bikeshedding
}); That means that v-p-s does something like this const pkg = JSON.parse(fs.readFileSync('svelte/package.json', 'utf8'));
export default defineConfig({
resolve: {
alias: [
{
find: /^svelte\/?/,
customResolver: (id, importer) => {
// For some reason this turns up as "undefined" instead of "svelte/"
const exported = pkg.exports[id === 'undefined' ? '.' : id.replace('undefined', './')];
if (!exported) return;
// When running the server version of the Svelte files,
// we also want to use the server export of the Svelte package
return path.resolve(
'packages/svelte',
/* some conditional if people don't want this for certain files */
? exported.default
: exported.browser ?? exported.default
);
}
}
]
}
}); |
according to https://github.com/rollup/plugins/tree/master/packages/alias#custom-resolvers customResolver is a customized instance of rollup plugin node-resolve, not a resolve function. The |
Eagerly in this module
which is re-exported in
But JSDom actually supply it...the problem is when you have a file that doesn't need JSDom (because it's just testing some utility for example but that utility is exported from a file where svelte is also imported from some unrelated stuff) |
#12133 mentions you can override why do we rely on BROWSER instead of |
That would throw a reference error, you would at least need: globalThis.requestAnimationFrame ?? noop
// or
typeof requestAnimationFrame == 'undefined' ? noop : requestAnimationFrame |
Does
One reason is for smaller bundle size. You're making the browser runtime larger if you have to add |
I think it does but again the problem is for the files where you don't have to use JSDom because is just js. |
Really what we want is a way to say that we're running browser code in Node. What if we had testing library svelte specify |
Yeah not sure why this was marked as closed, we need to document the alias/browser version thing - did you adjust that in your config? |
Yeah it works now. I would also like to point out that it's not very clear in the docs that testing runic code requires the JSDom environment. I tried wrapping code that uses https://svelte.dev/docs/svelte/testing JSDom was mentioned in the section for component testing, so I had thought it was not necessary. EDIT: ![]() |
to be clear you don't need a JSDOM environment, you need a browser environment. |
The browser condition is also necessary to test runes, so it makes sense to add it to the first occurence to the vite config. Also add a note about more fine-grained alias conditions. Closes #13961
* docs: note browser condition earlier The browser condition is also necessary to test runes, so it makes sense to add it to the first occurence to the vite config. Also add a note about more fine-grained alias conditions. Closes #13961 * Update documentation/docs/07-misc/02-testing.md --------- Co-authored-by: Rich Harris <[email protected]>
Describe the bug
The following test fails.
This issue only happens in Vitest. If you run the reproduction provided below, you will find that the same code works fine in the browser. Both values are the same, as expected.
Reproduction
https://github.com/abdel-17/svelte-vitest-set-bug-repro
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: