-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Svelte 5] Functions using onMount
fail tests due no component initialization
#13136
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
I get that a component passed to React has ways to test their hooks. I'm looking for a way to test these reactive functions that contain lifecycle and runes as the Svelte counterpart. |
Here's an example with $effect.root and as you can see i'm able to call the function in the module itself without the error. Now this might be fine since the effect is decently simple, doesn't have any dependency and it will be garbage collected when the page closes. If you want a more involved solution you could keep count of the subscribers inside the getters and only register an effect if someone is actually getting the value from within an effect. You can use |
This will also be covered in our new docs, you can take a peek here: https://github.com/sveltejs/svelte/blob/main/documentation/docs/05-misc/02-testing.md#using-runes-inside-your-test-files Closing because this is not a bug within Svelte |
Thanks for the explanation on this @paoloricciuti. That was one of the runes I breezed over but now it's worth taking a deeper look. If you don't mind, I would also like to see a sample of tracking subscribers within getters if the value is actually intended to be read. I'm curious how that would operate. @dummdidumm Thank you for sharing the write up on this exact scenario. Those are very helpful. I would only ask one thing related to the document you linked. In there, there is a reference to these modules which is being used for the tests: import { multiplier } from './multiplier.svelte.js'; // used in multiplier.svelte.test.js
import { logger } from './logger.svelte.js'; // used in logger.svelte.test.js It would be nice to see the implementation of Thanks for taking a look at my issue and providing me guidance. I really appreciate it. |
Weird because I can't get I removed the Edit : it's not fine since I have other functions that need a cleanup... |
Describe the bug
Functions (not components) that call
onMount
throws an error. The error states thatonMount
can only be used in component initialization.The purpose of calling
onMount
in a function is so that mounting logic can be done on behalf of the component. The attached StackBlitz project showcases an example.Reproduction
Linked below is a working sample. In summary, there is a
useMouseCoords
function that attachesmousemove
listeners in anonMount
function that updates state runes. That function is then used insideApp.svelte
to read those state runes after calling theonMount
function.https://stackblitz.com/edit/vitejs-vite-2fltxz?file=tests%2Fone.test.ts
Function sample
Test sample
Logs
System Info
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 18.20.3 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.2.3 - /usr/local/bin/npm pnpm: 8.15.6 - /usr/local/bin/pnpm npmPackages: svelte: ^5.0.0-next.243 => 5.0.0-next.243
Severity
annoyance
The text was updated successfully, but these errors were encountered: