diff --git a/packages/nuxt-vitest/src/module.ts b/packages/nuxt-vitest/src/module.ts index 632773a0..5f1ac8ce 100644 --- a/packages/nuxt-vitest/src/module.ts +++ b/packages/nuxt-vitest/src/module.ts @@ -7,11 +7,13 @@ import { getPort } from 'get-port-please' import { h } from 'vue' import { debounce } from 'perfect-debounce' import { isCI } from 'std-env' +import { resolve } from "path" export interface NuxtVitestOptions { startOnBoot?: boolean logToConsole?: boolean vitestConfig?: VitestConfig + vitestConfigPath?: string } /** @@ -78,6 +80,7 @@ export default defineNuxtModule({ }) process.env.__NUXT_VITEST_RESOLVED__ = 'true' + const { startVitest } = (await import( pathToFileURL(await resolvePath('vitest/node')).href )) as typeof import('vitest/node') @@ -98,10 +101,14 @@ export default defineNuxtModule({ const watchMode = !process.env.NUXT_VITEST_DEV_TEST && !isCI + const getUserConfig = options.vitestConfigPath ? (await import(resolve(options.vitestConfigPath))) : () => ({}) + + const userConfig = await getUserConfig() // For testing dev mode in CI, maybe expose an option to user later const vitestConfig: VitestConfig = watchMode ? { passWithNoTests: true, + ...userConfig.test, ...options.vitestConfig, reporters: options.logToConsole ? [ @@ -117,6 +124,7 @@ export default defineNuxtModule({ }, } : { + ...userConfig.test, ...options.vitestConfig, watch: false, } diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index a4407a9b..5a51061e 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -8,6 +8,7 @@ export default defineNuxtConfig({ vitest: { startOnBoot: true, logToConsole: true, + vitestConfigPath: "./vitest.config", vitestConfig: { setupFiles: ['./tests/setup/mocks'] } diff --git a/playground/tests/setup/global.ts b/playground/tests/setup/global.ts new file mode 100644 index 00000000..65afcd7c --- /dev/null +++ b/playground/tests/setup/global.ts @@ -0,0 +1,3 @@ +import { vi } from "vitest" + +vi.stubEnv('stubbed', 'true') \ No newline at end of file diff --git a/playground/tests/unit/setupMocks.spec.ts b/playground/tests/unit/setupMocks.spec.ts new file mode 100644 index 00000000..ef2444c7 --- /dev/null +++ b/playground/tests/unit/setupMocks.spec.ts @@ -0,0 +1,8 @@ +import { expect, describe, test } from "vitest" + + +describe('test mock in setup file', () => { + test('stubbed env to be "true"', () => { + expect(process.env.stubbed).toBe('true') + }) +}) \ No newline at end of file diff --git a/playground/vitest.config.ts b/playground/vitest.config.ts index 6f641c10..f1334fa2 100644 --- a/playground/vitest.config.ts +++ b/playground/vitest.config.ts @@ -12,7 +12,7 @@ export default defineVitestConfig({ rootDir: fileURLToPath(new URL('./', import.meta.url)), domEnvironment: process.env.VITEST_DOM_ENV as 'happy-dom' | 'jsdom' ?? 'happy-dom', }, - }, - setupFiles: ['./tests/setup/mocks.ts'] + }, + setupFiles: ['./tests/setup/global', './tests/setup/mocks.ts'] }, })