Skip to content

Commit

Permalink
fix: set rootId when module is enabled (and set test)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 28, 2023
1 parent 9e46795 commit 4d8e3eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/nuxt-vitest/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ async function startNuxtAndGetViteConfig(
overrides: defu(
{
ssr: false,
app: {
rootId: 'nuxt-test',
},
test: true,
},
overrides
),
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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
}
}
}
4 changes: 4 additions & 0 deletions src/nuxt-vitest/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default defineNuxtModule<NuxtVitestOptions>({

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

Expand Down
2 changes: 1 addition & 1 deletion src/vitest-environment-nuxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default <Environment>{

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 =
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/nuxt-vitest/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit 4d8e3eb

Please sign in to comment.