S
SolidJS•12mo ago
oneiro

Are global signals ever reset between test runs?

@lexlohr Sorry - another issue 😅 As I've described in another issue I have structured my application in a way, that my global state mainly consists of global stores. Now to create tests I usually need to first populate my stores with some test data. This does seem to work fine in general, howerver I noticed, that apparently these stores will keep their state between tests. Is this a mistake on my end, am I using stores wrong, or is this a bug?
13 Replies
foolswisdom
foolswisdom•12mo ago
My guess is that the file in which the signal is defined would need to be imported again in order to reset the state, and I doubt that any test would do that (it would probably slow it down a lot) Which would be interesting, because testing is not a downside we usually mention when discussing global signals
oneiro
oneiro•12mo ago
hm, is there a better way to handle state in solid, then? Or would I have to write reset functions which are always called after each test (this seems to be a pretty heavy and boilerplaty solution)
foolswisdom
foolswisdom•12mo ago
The usual alternative is to use a context provider And put the provider at the root of your app Unfortunately, for component tests, you'd need to make sure that you render the component wrapped in the provider Which results in some boilerplate
oneiro
oneiro•12mo ago
Hm, yeah this is still a lot of manual wiring 😦 I really like that I can just place a store whereever I want and consume it. I consider contexts only in cases, where I want to make sure the state is scoped to a specific part of the component tree
foolswisdom
foolswisdom•12mo ago
Right, that make sense
oneiro
oneiro•12mo ago
Hm, as it stands I will probably have to manually reset my stores then 😬
foolswisdom
foolswisdom•12mo ago
I'm not sure there's really a better option for global state 🤷
Alex Lohr
Alex Lohr•12mo ago
That's a real good point. Mmh, this shall require some thought, but I might be able to create something that allows us to store and restore states between tests in our testing library. I'm not sure how I'll do it, though, so don't expect a fast solution.
oneiro
oneiro•12mo ago
One odd thing I noticed (but this might be a misunderstanding of stores on my end): When I try to fully reset a store, this still won't update the store correctly. For example:
const createInitialState = () => ({})
const [myStore, setMyStore] = createStore<Record<string, { id: string }>(createInitialState())|
const resetStore = () => setMyStore(createInitialState())
const createInitialState = () => ({})
const [myStore, setMyStore] = createStore<Record<string, { id: string }>(createInitialState())|
const resetStore = () => setMyStore(createInitialState())
This seems to work in our running app, but in tests stores won't be emptied and still keep their state. Any idea what I could be doing wrong here? just calling the setter on a property updates the store accoringly, e.g. setMyStore('SOME_ID", { id: 'UPDATE_ID' }) apparently reconcile seems to help, but do I actually need this here?
Alex Lohr
Alex Lohr•12mo ago
Yes, you basically have to set every root property to undefined yourself.
oneiro
oneiro•12mo ago
and reconcile does this? (I am still a bit confused about reconcile and what it actually does)
foolswisdom
foolswisdom•12mo ago
Reconcile transforms the store to match the shape and values of the value passed to the reconcile function. The advantage of that (vs just setting to an empty object) is 1. it only updates properties that need to be updated 2. it also deletes properties that don't exist in the target object, while the regular setter merges (but shallowly!) the current store and the object passed to setStore
oneiro
oneiro•12mo ago
Ah I see. Thanks! So if I understand that correctly, reconcile could change the underlying store completely (speaking in static types, it could change to a different type). However, if the target object contains properties which are identical in the current store, no updates will be triggered. And properties which are no longer present in the reconciled store will be removed instead of being kept. Therefore handing an empty object to reconcile will remove all properties. Correct? 🙂 I think it would be nice to have some more of these "how to properly update a store"-real world examples in the docs. The description of functions like reconcile is currently very technical and hard to grasp. (Same thing for fns like runWithOwner etc., to get a better understanding when and how these are actually being used) I would love to contribute, but I don't quite get most of these myself, yet 😅
Want results from more Discord servers?
Add your server
More Posts
How to have a default layout for all routes?I am using `@solidjs/router` and I want to have a default layout to have a default background color.Why does effect re-fire? How to use createStore values in effect correctly?Check out this component. Why does the effect refire when I'm only changing person.name.first? It Access native html dialogs show/close methods in clickHandlerHey folks, I am currently using native html dialogs inside my app and am accessing their show/closeUncaught ReferenceError: createDeepSignal is not definedNot really sure what I'm doing wrong here. I definitely have SolidJS >v1.5 and I'm using a close co__vite_ssr_import_0__.default is not a function only on API endpointsI am getting an error from my api end-points when importing a third party library in my API code. I Why is storageSignal possibly null?I am having trouble understanding why TypeScript thinks that this storageSignal `theme` can be null:Is there a way to debug how callbacks are called, when firing an event with testing-library?[SOLVED]Hey, I am having a hard time testing my code and would like to find out if and how some of my eventUnderstanding Suspense in SolidStart SSRTo understand how Suspense behave in a SolidStart SSR project, I have project created using the SoliSolidStart debugging in VSCodeHow can I debug (mean put breakpoints and step over code) SolidStart project in VSCode? I am currenWhat's the best way to make a docs website?I'm building a SolidJS library and want to make a simple docs website for it. I'm hoping to leverage