TypeError when using `createResource` in vitest
I'm new to Solidjs, and I'm learing how to test functions in vitest, it cause the error
TypeError: Cannot read properties of undefined (reading 'id')
, does anyone know how to solve it? there is no solution when asking ChatGPTš„²
my packages
6 Replies
what is your vitest config?
it looks like your error does not refer to the test that you have shown.
because it tries to read [something].id when [something] is undefined.
full error log is here
You need to setup the resolver to test in dev mode:
Sorry for the late reply by the way, I was swamped with meetings.
thanks! it works!
btw, I wrap the test code with
createRoot
, as the warning say computations created outside a
createRoot or
render will never be disposed
If you want to test effects asynchronously, have a look at testEffect from our testing library (I'm pretty happy I added it).
Otherwise, createRoot will suffice.
That's the pattern that I use to test resources: https://github.com/solidjs-community/solid-primitives/blob/main/packages/resource/test/index.test.ts#L178
GitHub
solid-primitives/index.test.ts at main Ā· solidjs-community/solid-pr...
A library of high-quality primitives that extend SolidJS reactivity. - solid-primitives/index.test.ts at main Ā· solidjs-community/solid-primitives