Skip to content

Commit 1431d24

Browse files
authored
test: stub color mode helper (#229)
1 parent 1a26c25 commit 1431d24

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

test/nuxt/setup.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Global setup for Nuxt vitest environment.
3+
*
4+
* This file stubs globals that are normally initialized by inline scripts
5+
* injected into the HTML document, which don't run in vitest's nuxt environment.
6+
*/
7+
8+
// Stub the @nuxtjs/color-mode global helper
9+
// The color-mode module injects a script that initializes this on window,
10+
// but in vitest the script never runs since the HTML page isn't SSR'd.
11+
// See: https://github.com/nuxt-modules/color-mode/issues/335
12+
if (typeof window !== 'undefined') {
13+
const globalName = '__NUXT_COLOR_MODE__'
14+
15+
// @ts-expect-error - dynamic global name
16+
if (!window[globalName]) {
17+
// @ts-expect-error - dynamic global name
18+
window[globalName] = {
19+
preference: 'system',
20+
value: 'dark',
21+
getColorScheme: () => 'dark',
22+
addColorScheme: () => {},
23+
removeColorScheme: () => {},
24+
}
25+
}
26+
}

vitest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default defineConfig({
2525
name: 'nuxt',
2626
include: ['test/nuxt/**/*.{test,spec}.ts'],
2727
environment: 'nuxt',
28+
setupFiles: ['./test/nuxt/setup.ts'],
2829
environmentOptions: {
2930
nuxt: {
3031
rootDir: fileURLToPath(new URL('.', import.meta.url)),
@@ -59,6 +60,7 @@ export default defineConfig({
5960
'shared/utils/spdx.ts',
6061
'shared/utils/url.ts',
6162
'server/utils/readme.ts',
63+
'server/utils/docs/text.ts',
6264
'server/utils/code-highlight.ts',
6365
'server/utils/npm.ts',
6466
'server/utils/shiki.ts',

0 commit comments

Comments
 (0)