diff --git a/test/nuxt/setup.ts b/test/nuxt/setup.ts new file mode 100644 index 0000000000..1be06bd962 --- /dev/null +++ b/test/nuxt/setup.ts @@ -0,0 +1,26 @@ +/** + * Global setup for Nuxt vitest environment. + * + * This file stubs globals that are normally initialized by inline scripts + * injected into the HTML document, which don't run in vitest's nuxt environment. + */ + +// Stub the @nuxtjs/color-mode global helper +// The color-mode module injects a script that initializes this on window, +// but in vitest the script never runs since the HTML page isn't SSR'd. +// See: https://github.com/nuxt-modules/color-mode/issues/335 +if (typeof window !== 'undefined') { + const globalName = '__NUXT_COLOR_MODE__' + + // @ts-expect-error - dynamic global name + if (!window[globalName]) { + // @ts-expect-error - dynamic global name + window[globalName] = { + preference: 'system', + value: 'dark', + getColorScheme: () => 'dark', + addColorScheme: () => {}, + removeColorScheme: () => {}, + } + } +} diff --git a/vitest.config.ts b/vitest.config.ts index cf3b5d6399..838d144f17 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -25,6 +25,7 @@ export default defineConfig({ name: 'nuxt', include: ['test/nuxt/**/*.{test,spec}.ts'], environment: 'nuxt', + setupFiles: ['./test/nuxt/setup.ts'], environmentOptions: { nuxt: { rootDir: fileURLToPath(new URL('.', import.meta.url)), @@ -59,6 +60,7 @@ export default defineConfig({ 'shared/utils/spdx.ts', 'shared/utils/url.ts', 'server/utils/readme.ts', + 'server/utils/docs/text.ts', 'server/utils/code-highlight.ts', 'server/utils/npm.ts', 'server/utils/shiki.ts',