mocking useI18n with nuxt i18n module
What previously worked within my tests to handle mocking
useI18n
calls no longer does.
I get the error Must be called at the top of a
setup function
How frustrating!3 Replies
Hi @dmarr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I understand your frustration with mocking
useI18n
in your Nuxt tests. This is a common issue when testing components that use the i18n module.
The error "Must be called at the top of a setup
function" occurs because composables like useI18n
need to be called within a component's setup context. When you're writing tests, this context isn't automatically available.
Based on the knowledge sources, there are a few approaches to mock the useI18n
function in your tests:
Using mockNuxtImport
The recommended approach is to use mockNuxtImport
from @nuxt/test-utils/runtime
:
This approach is documented in the Nuxt Testing documentation and is the most straightforward way to mock Nuxt's auto-imports.
For Different Implementations Between Tests
If you need different implementations between tests, you can use vi.hoisted
:
Remember that recent changes in Nuxt might have affected how mocking works. The knowledge sources mention a PR (#31295) that changed how mocked exports work, which could be related to your issue.
Make sure you're using the latest version of @nuxt/test-utils
and check if there are any specific requirements for your version of Nuxt.
Let me know if you need more specific help with your testing setup!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@139883350096740354> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Also on the docs for mockNuxtImport: https://nuxt.com/docs/getting-started/testing#mocknuxtimport
The suggestion:
if i wanted to pu those parts in the vitest global setup, how would I get access to useStorageMock?