vue.jsnuxt.jsvitestkey-value-storenitro

Mocking useStorage() using @nuxt/test-utils


The goal is to write a test for a function that uses KV storage, under the hood provided by Nitro, in the Nuxt app. On the client side useStorage() is automatically imported. When writing a test, documentation recommends mocking the function.

After copying the snippet from the documentation as:

mockNuxtImport('useStorage', () => {
  return () => {
    return { value: 'mocked storage' }
  }
})

The test method fails with Error: Cannot find import "useStorage" to mock.

What should be imported to successfully mock the useStorage()?


Solution

  • The mock is not possible because useStorage is accessible only from a scope of Nitro which is at ~/server. Thus different approach must be used to test this method.