CANNOT RESOLVE #app & #imports from vitest into my nuxt-module
Hello everyone, i'm recently working on my own nuxt-module. And I want to add some tests. But I've some imports from "#imports" or "#app" (like useNuxtApp) into my nuxt module, and these type of imports raised some errors when i'm running tests. I tryied to add some alliases into my vitest.config.ts, but nothing changed.
2 Replies
Hi @nath_an_v. 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 resolving
#app
and #imports
aliases in your Nuxt module tests. This is a common issue when testing Nuxt modules with Vitest.
Based on the knowledge sources, there are a few approaches to handle this:
Use mockNuxtImport
for auto-imports
The recommended way to handle Nuxt auto-imports in tests is to use mockNuxtImport
from @nuxt/test-utils/runtime
:
This approach is specifically designed for mocking Nuxt's auto-import functionality in tests Nuxt Testing Documentation.
Set up the Nuxt testing environment
Make sure you've properly set up the Nuxt testing environment:
1. Install the required dependencies:
2. Add @nuxt/test-utils/module
to your nuxt.config.ts
(optional):
3. Create a vitest.config.ts
using defineVitestConfig
:
4. Make sure your test files use the Nuxt environment by either:
- Adding .nuxt.
to the test filename (e.g., my-file.nuxt.test.ts
)
- Adding // @vitest-environment nuxt
as a comment in the test file
This setup ensures that your tests run in a proper Nuxt environment Nuxt Testing Documentation.
Recent issues with alias resolution
There have been some recent issues with Nuxt alias resolution, particularly with #app
imports. A recent PR (#30148) addressed issues with nuxt
and nuxt/app
resolutions, which might be related to your problem GitHub PR #30148.
Make sure you're using the latest version of Nuxt and its testing utilities, as this might resolve your issue.
If you're still experiencing problems, you might need to provide more specific error messages and details about your setup for more targeted assistance.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@887246763700928513> Kapa.ai is still learning and improving, please let me know how I did by reacting below.