Auto importing in unit tests
In Nuxt 4.2.0 it's great that we have auto-imports, but it doesn't work in the unit tests. So if I e.g. have
app/utils/helper.ts it works great using it across the Nuxt application, but it will fail in the tests if it e.g. uses another util inside that is auto-imported. Isn't there some way to have the auto-imports be available in the test/unit as well?2 Replies
Hi @Mathias Rando. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources, there is indeed a solution for making auto-imports available in unit tests in Nuxt 4.
According to the Nuxt 4 - Alias imports not recognized when running tests issue, the key is to use
defineVitestProject to properly configure your test setup. The documentation example was missing this critical detail.
Here's how to configure your tests to support auto-imports:
The important part is wrapping your test configuration with await defineVitestProject({...}). This configures your project from the Nuxt config and makes auto-imports (including those from app/utils/) available in your tests.
As noted in the issue discussion, once you wrap your unit test config with await defineVitestProject, the alias and auto-import issues are resolved, and your tests will be able to use auto-imported utilities just like the rest of your Nuxt application.
Note: There may still be TypeScript errors shown in your IDE for alias imports in .spec.ts or .test.ts files, but the tests should run and pass correctly.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@537569149883252756> Kapa.ai is still learning and improving, please let me know how I did by reacting below.