Mocking Pinia Store in Vitest Component Test
I would like to test my Nuxt3 component with vitest and nuxt/test-utils. This component uses a Pinia store.
Component (simplified):
Store (simplified):
I've now tried to test it as described on the Pina page about testing.
Test:
Both assertions fail, the first one with
AssertionError: expected "spy" to be called once, but got 0 times
. Its like my component does not pick up the testing Pinia instance for some reason.1 Reply
Thanks for the idea!
I was able to mock it with
mockNuxtImport()
and vitest
's mock functionality.
That does the trick.
In my component, I utilize callOnce, and it appears to truly execute only once within a single test file. That's why retrieveAllLeagues
isn't restored with mockRestore()
. I suppose if someone wants to ensure the correct methods are called with callOnce
, they either need to assert them in the proper sequence or employ multiple test files.