N
Nuxt7mo ago
w7a9q

useStorage in nuxt/test-utils

I'm trying to use useStorage() in vitest unit test. I'm using nuxt/test-utils package.
it('should return bool', async () => {
const code = 'XF4'
const result = await askTransistor(code);

const { getItem } = useStorage('trn_data')
const x = getItem(code)

expect(x).toEqual(true)
})
it('should return bool', async () => {
const code = 'XF4'
const result = await askTransistor(code);

const { getItem } = useStorage('trn_data')
const x = getItem(code)

expect(x).toEqual(true)
})
Normally in my code useStorage() is auto imported but in the test it's not the case. How can I make it work? This snipped fails at: ReferenceError: useStorage is not defined
5 Replies
Tirius
Tirius7mo ago
Bump. I'm interested in solution too. How to handle/mock auto imported composables.
manniL
manniL7mo ago
I'd mock useStorage here instead, e.g. via mockNuxtImport
w7a9q
w7a9qOP7mo ago
That doesn't solve the issue when I want to test a function that saves an item to the storage. I want to load the item afterwards and perform assertion on it.
manniL
manniL7mo ago
but then you test the behavior of useStorage which isn't under your control but what you could do is add a route only available in test which returns that item via useStorage. that might be a workaround if you really need it
w7a9q
w7a9qOP7mo ago
So you think it's bad designed test? The point of the function is to process the object and save some of the properties to the KV. I need to verify that the content saved into the KV is correct.
Want results from more Discord servers?
Add your server