Nuxt 4 Migration, have build working, tests fail
I'm working on a migration to Nuxt 4, I've done the outlined file changes to be in the
./app
directory. My build work and app seems mostly fine, tests however:
It looks like it's trying to somehow look for a vitest.setup.ts
in the /app
directory rather than where it seemingly should be which would be the root
of the repo.
Not sure how to fix this configuration issue so the tests reference into the setup file/ config files at the root.3 Replies
Hi @Buffet Time. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
It’s likely caused by the new Nuxt 4 directory defaults: the srcDir is now app/, and the ~ alias points to app/. If your Vitest setup path is referenced via ~ (or resolved relative to srcDir), it will look under app/. Place test-related files at the root and reference them with a root-based path/alias.
Try one of the following:
- Move the setup file to app/vitest.setup.ts (if you intend to keep using ~ paths that now resolve to app/) (New Directory Structure).
- Keep vitest.setup.ts at the repo root and reference it with a root alias () or a relative path in your Vitest config so it doesn’t resolve via srcDir. In Nuxt 4, ~ points to app, while points to the project root (New Directory Structure; see also similar alias note in a closed issue: using for root vs ~ for app) (Cant read css file).
Additionally, Nuxt 4 recommends using Vitest projects for clearer separation of environments. Adopting the documented Vitest projects setup can avoid path/alias confusion and misconfigurations (Unit Testing > Setup; docs PR note on Vitest projects).
If you also auto-imported types from a types folder at the root in Nuxt 3, note that imports are now resolved relative to srcDir (app/) by default; use ~/types for app/types, or for a root-level types folder, or place shared types in shared/ (Auto-import types issue; New Directory Structure).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@136494200391729152> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
setupFiles: ['vitest.setup.ts']
-> setupFiles: ['../vitest.setup.ts']
fixes it.