Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
feat(getVitestConfig): pass custom nuxt instance and config to `getVi…
Browse files Browse the repository at this point in the history
…testConfig` (#22)
  • Loading branch information
antfu authored Jan 13, 2023
1 parent 19d103d commit 3e5b470
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
"dependencies": {
"@nuxt/kit": "^3.0.0",
"@vue/test-utils": "^2.2.6",
"estree-walker": "^3.0.1",
"h3": "^1.0.2",
"happy-dom": "^8.1.0",
"ofetch": "^1.0.0",
"magic-string": "^0.27.0",
"estree-walker": "^3.0.1",
"ofetch": "^1.0.0",
"unenv": "^1.0.0"
},
"devDependencies": {
Expand Down
28 changes: 18 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { loadNuxt, buildNuxt } from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { InlineConfig as VitestConfig } from 'vitest'
import { InlineConfig, mergeConfig, defineConfig } from 'vite'
import modules from './module'

interface GetVitestConfigOptions {
nuxt: Nuxt
viteConfig: InlineConfig
}

// https://github.com/nuxt/framework/issues/6496
async function getNuxtAndViteConfig(rootDir = process.cwd()) {
const { loadNuxt, buildNuxt } = await import('@nuxt/kit')
const nuxt = await loadNuxt({
cwd: rootDir,
dev: false,
Expand All @@ -20,9 +25,9 @@ async function getNuxtAndViteConfig(rootDir = process.cwd()) {
nuxt.options.modules.push(modules)
await nuxt.ready()

return new Promise<{ nuxt: Nuxt, config: InlineConfig }>((resolve, reject) => {
nuxt.hook('vite:extendConfig', config => {
resolve({ nuxt, config })
return new Promise<GetVitestConfigOptions>((resolve, reject) => {
nuxt.hook('vite:extendConfig', viteConfig => {
resolve({ nuxt, viteConfig })
throw new Error('_stop_')
})
buildNuxt(nuxt).catch(err => {
Expand All @@ -33,13 +38,13 @@ async function getNuxtAndViteConfig(rootDir = process.cwd()) {
}).finally(() => nuxt.close())
}

export async function getVitestConfig(): Promise<
InlineConfig & { test: VitestConfig }
> {
const { config: viteConfig, nuxt } = await getNuxtAndViteConfig()
export async function getVitestConfig(
options?: GetVitestConfigOptions
): Promise<InlineConfig & { test: VitestConfig }> {
if (!options) options = await getNuxtAndViteConfig()

return {
...viteConfig,
...options.viteConfig,
test: {
environment: 'nuxt',
deps: {
Expand All @@ -50,12 +55,15 @@ export async function getVitestConfig(): Promise<
// additional deps
'vue',
'vitest-environment-nuxt',
...nuxt.options.build.transpile.filter(r => typeof r === 'string' || r instanceof RegExp) as Array<string | RegExp>,
...(options.nuxt.options.build.transpile.filter(
r => typeof r === 'string' || r instanceof RegExp
) as Array<string | RegExp>),
],
},
},
}
}

export async function defineConfigWithNuxtEnv(config: InlineConfig = {}) {
return defineConfig(async () => {
return mergeConfig(await getVitestConfig(), config)
Expand Down

0 comments on commit 3e5b470

Please sign in to comment.