The knowledge sources do not explicitly document a built-in or recommended way to mock $fetch requests in Nuxt unit tests to avoid 404 errors. However, there are related examples for mocking composables and auto-imported functions in Vitest using mockNuxtImport, which is the preferred approach for mocking Nuxt auto-imports in tests.
For example, to mock a composable like useSupabaseClient, you can use:
import { mockNuxtImport } from 'nuxt-vitest/utils'
mockNuxtImport('useSupabaseClient', () => {
return () => ({
})
})
How to mock Supabase in Vitest?
While there is no direct example for $fetch, you could try a similar approach by mocking $fetch using mockNuxtImport:
import { mockNuxtImport } from 'nuxt-vitest/utils'
mockNuxtImport('$fetch', () => {
return async (url, options) => {
return { items: [], basketId: '123' }
}
})
This should intercept $fetch calls in your tests and prevent 404 errors, as the real network request will not be made.
If this approach does not work for your setup, the knowledge sources do not provide further details or alternatives.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: