Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/nuxt-vitest/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -78,6 +80,7 @@ export default defineNuxtModule<NuxtVitestOptions>({
})

process.env.__NUXT_VITEST_RESOLVED__ = 'true'

const { startVitest } = (await import(
pathToFileURL(await resolvePath('vitest/node')).href
)) as typeof import('vitest/node')
Expand All @@ -98,10 +101,14 @@ export default defineNuxtModule<NuxtVitestOptions>({

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
? [
Expand All @@ -117,6 +124,7 @@ export default defineNuxtModule<NuxtVitestOptions>({
},
}
: {
...userConfig.test,
...options.vitestConfig,
watch: false,
}
Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineNuxtConfig({
vitest: {
startOnBoot: true,
logToConsole: true,
vitestConfigPath: "./vitest.config",
vitestConfig: {
setupFiles: ['./tests/setup/mocks']
}
Expand Down
3 changes: 3 additions & 0 deletions playground/tests/setup/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { vi } from "vitest"

vi.stubEnv('stubbed', 'true')
8 changes: 8 additions & 0 deletions playground/tests/unit/setupMocks.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
4 changes: 2 additions & 2 deletions playground/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
},
})