Skip to content

Commit 5ada22a

Browse files
committed
refactor(module): extract nuxt environment options plugin
1 parent c19a659 commit 5ada22a

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/config.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { defu } from 'defu'
1212
import { createResolver, findPath } from '@nuxt/kit'
1313

1414
import { applyEnv, loadKit } from './utils'
15+
import { NuxtVitestEnvironmentOptionsPlugin } from './module/plugins/options'
1516

1617
interface GetVitestConfigOptions {
1718
nuxt: Nuxt
@@ -307,22 +308,7 @@ async function resolveConfig<T extends ViteUserConfig & { test?: VitestConfig }
307308
}) satisfies ViteUserConfig & { test: NonNullable<T['test']> },
308309
) as T & { test: NonNullable<T['test']> }
309310

310-
const PLUGIN_NAME = 'nuxt:vitest:nuxt-environment-options'
311-
const STUB_ID = 'nuxt-vitest-environment-options'
312-
resolvedConfig.plugins!.push({
313-
name: PLUGIN_NAME,
314-
enforce: 'pre',
315-
resolveId(id) {
316-
if (id.endsWith(STUB_ID)) {
317-
return STUB_ID
318-
}
319-
},
320-
load(id) {
321-
if (id.endsWith(STUB_ID)) {
322-
return `export default ${JSON.stringify(resolvedConfig.test.environmentOptions || {})}`
323-
}
324-
},
325-
})
311+
resolvedConfig.plugins!.push(NuxtVitestEnvironmentOptionsPlugin(resolvedConfig.test.environmentOptions))
326312

327313
if (resolvedConfig.test.browser?.enabled) {
328314
if (resolvedConfig.test.environment === 'nuxt') {

src/module/plugins/options.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Plugin } from 'vite'
2+
import type { EnvironmentOptions } from 'vitest/node'
3+
4+
const PLUGIN_NAME = 'nuxt:vitest:nuxt-environment-options'
5+
const STUB_ID = 'nuxt-vitest-environment-options'
6+
7+
export function NuxtVitestEnvironmentOptionsPlugin(environmentOptions: EnvironmentOptions = {}): Plugin {
8+
return {
9+
name: PLUGIN_NAME,
10+
enforce: 'pre',
11+
resolveId(id) {
12+
if (id.endsWith(STUB_ID)) {
13+
return STUB_ID
14+
}
15+
},
16+
load(id) {
17+
if (id.endsWith(STUB_ID)) {
18+
return `export default ${JSON.stringify(environmentOptions || {})}`
19+
}
20+
},
21+
}
22+
}

0 commit comments

Comments
 (0)