Skip to content

Multiple nuxt environment projects collide if imported via vitest projects #1419

@fimion

Description

@fimion

Environment

  • Operating System: Darwin
  • Node Version: v22.17.0
  • Nuxt Version: 4.1.2
  • CLI Version: 3.28.0
  • Nitro Version: 2.12.6
  • Package Manager: npm@11.6.0
  • Builder: -
  • User Config: compatibilityDate, devtools, modules
  • Runtime Modules: @nuxt/test-utils@3.19.2
  • Build Modules: -

Reproduction

https://github.com/fimion/nuxt-test-utils-projects-problem

Describe the bug

When using defineConfig from vitest/config to glob import project definitions from separate config files, defineVitestProject will error out if it is used multiple times.

Example, component testing with auto mocked data vs actual endpoints.

The issue has something to do with how defineVitestProject gets run when dynamically imported by vitest and defineVitestProject will attempt to spin up a nuxt instance to get the configuration. Multiple instances attempt to run at the same time, and this causes anything after the first project read in to fail.

Additional context

We ran into this issue as we were attempting to create a way to quickly add new testing setups in a monorepo. The solution for us was one of a couple of possibilities:

  • wrap defineVitestProject in a call using a curried method call created by limitConcurrency(1) from @vitest/runner/utils which causes each call of defineVitestProject to be run in sequence rather than simultaneously.
  • similarly, create a shared chain of promises that causes each call of defineVitestProject to run in sequence.
  • manually create a vite-node instance and read in the projects definitions manually before vitest can attempt to run it's process. then await each call of defineVitestProject manually.

This may be more of an upstream issue? I'm not entirely sure.

Logs

> test
> vitest --run


 RUN  v3.2.4 /Users/alex.riviere/WebstormProjects/nuxt-test-utils-project-collision

 ✓  Unit  app/components/HelloWorld.unit.ts (1 test) 7ms

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL   Integration  app/components/HelloWorld.integration.ts [ app/components/HelloWorld.integration.ts ]
Error: Failed to resolve import "#app/nuxt-vitest-app-entry" from "node_modules/@nuxt/test-utils/dist/runtime/shared/nuxt.mjs". Does the file exist?
  Plugin: vite:import-analysis
  File: /Users/alex.riviere/WebstormProjects/nuxt-test-utils-project-collision/node_modules/@nuxt/test-utils/dist/runtime/shared/nuxt.mjs:3:15
  1  |  export async function setupNuxt() {
  2  |    const { useRouter } = await import("#app/composables/router");
  3  |    await import("#app/nuxt-vitest-app-entry").then((r) => r.default());
     |                 ^
  4  |    const nuxtApp = useNuxtApp();
  5  |    await nuxtApp.callHook("page:finish");
 ❯ TransformPluginContext._formatLog node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:31527:43
 ❯ TransformPluginContext.error node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:31524:14
 ❯ normalizeUrl node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:29996:18
 ❯ node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:30054:32
 ❯ TransformPluginContext.transform node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:30022:4
 ❯ EnvironmentPluginContainer.transform node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:31325:14
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-M_KD0XSK.js:26407:26

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed | 1 passed (2)
      Tests  1 passed (1)
   Start at  12:04:01
   Duration  736ms (transform 266ms, setup 200ms, collect 155ms, tests 7ms, environment 225ms, prepare 222ms)


Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions