From 908f96acda631e9c9cad1380bf3c74ebf70a280e Mon Sep 17 00:00:00 2001 From: julien huang Date: Thu, 27 Jul 2023 20:39:16 +0200 Subject: [PATCH 1/2] feat(nuxt-vitest): allow specifying vitest file config path --- packages/nuxt-vitest/src/module.ts | 8 ++++++++ playground/nuxt.config.ts | 1 + playground/tests/setup/global.ts | 3 +++ playground/tests/unit/setupMocks.spec.ts | 8 ++++++++ playground/vitest.config.ts | 1 + 5 files changed, 21 insertions(+) create mode 100644 playground/tests/setup/global.ts create mode 100644 playground/tests/unit/setupMocks.spec.ts 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 89240a06..1c5b855d 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" }, imports: { injectAtEnd: true, diff --git a/playground/tests/setup/global.ts b/playground/tests/setup/global.ts new file mode 100644 index 00000000..21749813 --- /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 67f7b134..af843b2c 100644 --- a/playground/vitest.config.ts +++ b/playground/vitest.config.ts @@ -13,5 +13,6 @@ export default defineVitestConfig({ domEnvironment: process.env.VITEST_DOM_ENV as 'happy-dom' | 'jsdom', }, }, + setupFiles: ['./tests/setup/global'] }, }) From 82be9d948b70fccbeeec01588e397cfc616cafbe Mon Sep 17 00:00:00 2001 From: julien huang Date: Thu, 27 Jul 2023 20:43:04 +0200 Subject: [PATCH 2/2] chore: lint --- playground/tests/setup/global.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/tests/setup/global.ts b/playground/tests/setup/global.ts index 21749813..65afcd7c 100644 --- a/playground/tests/setup/global.ts +++ b/playground/tests/setup/global.ts @@ -1,3 +1,3 @@ -import {vi} from "vitest" +import { vi } from "vitest" vi.stubEnv('stubbed', 'true') \ No newline at end of file