diff --git a/src/nuxt-vitest/config.ts b/src/nuxt-vitest/config.ts index 1d6418e2e..19a138f3f 100644 --- a/src/nuxt-vitest/config.ts +++ b/src/nuxt-vitest/config.ts @@ -22,9 +22,7 @@ async function startNuxtAndGetViteConfig( overrides: defu( { ssr: false, - app: { - rootId: 'nuxt-test', - }, + test: true, }, overrides ), @@ -111,6 +109,10 @@ export async function getVitestConfigFromNuxt( dir: process.cwd(), environmentOptions: { ...options.viteConfig.test?.environmentOptions, + nuxt: { + rootId: options.nuxt.options.app.rootId, + ...options.viteConfig.test?.environmentOptions?.nuxt, + }, nuxtRuntimeConfig: options.nuxt.options.runtimeConfig, }, environmentMatchGlobs: [ @@ -166,6 +168,11 @@ declare module 'vitest' { */ url?: string overrides?: NuxtConfig + /** + * The id of the root div to which the app should be mounted. You should also set `app.rootId` to the same value. + * @default {nuxt-test} + */ + rootId?: string } } } diff --git a/src/nuxt-vitest/module.ts b/src/nuxt-vitest/module.ts index 67c757d53..632773a08 100644 --- a/src/nuxt-vitest/module.ts +++ b/src/nuxt-vitest/module.ts @@ -34,6 +34,10 @@ export default defineNuxtModule({ if (!nuxt.options.dev) return + if (nuxt.options.test && nuxt.options.app.rootId === '__nuxt') { + nuxt.options.app.rootId = 'nuxt-test' + } + // the nuxt instance is used by a standalone Vitest env, we skip this module if (process.env.TEST || process.env.VITE_TEST) return diff --git a/src/vitest-environment-nuxt/index.ts b/src/vitest-environment-nuxt/index.ts index 905b36fc0..6288b4203 100644 --- a/src/vitest-environment-nuxt/index.ts +++ b/src/vitest-environment-nuxt/index.ts @@ -30,7 +30,7 @@ export default { const app = win.document.createElement('div') // this is a workaround for a happy-dom bug with ids beginning with _ - app.id = 'nuxt-test' + app.id = environmentOptions.nuxt.rootId win.document.body.appendChild(app) win.IntersectionObserver = diff --git a/test/fixtures/nuxt-vitest/nuxt.config.ts b/test/fixtures/nuxt-vitest/nuxt.config.ts index 6e99f9145..760d1432c 100644 --- a/test/fixtures/nuxt-vitest/nuxt.config.ts +++ b/test/fixtures/nuxt-vitest/nuxt.config.ts @@ -1,8 +1,5 @@ // https://v3.nuxtjs.org/api/configuration/nuxt.config export default defineNuxtConfig({ - app: { - rootId: 'nuxt-test', - }, modules: [ '../packages/nuxt-vitest/src/module', '@nuxt/devtools',