[READ-ONLY] Mirror of https://github.com/danielroe/nuxt-vitest. An vitest environment with support for testing code that needs a Nuxt runtime environment
nuxt nuxt-module testing unit-testing vitest
0

Configure Feed

Select the types of activity you want to include in your feed.

feat(getVitestConfig): pass custom nuxt instance and config to `getVitestConfig`

Anthony Fu (Jan 13, 2023, 2:37 PM +0100) 18e996ba 63c32922

+15 -8
+15 -8
src/config.ts
··· 1 - import { loadNuxt, buildNuxt } from '@nuxt/kit' 2 1 import type { Nuxt } from '@nuxt/schema' 3 2 import type { InlineConfig as VitestConfig } from 'vitest' 4 3 import { InlineConfig, mergeConfig, defineConfig } from 'vite' 5 4 import autoImportMock from './modules/auto-import-mock' 6 5 6 + export interface GetVitestConfigOptions { 7 + nuxt: Nuxt, 8 + viteConfig: InlineConfig, 9 + } 10 + 7 11 // https://github.com/nuxt/framework/issues/6496 8 12 async function getNuxtAndViteConfig(rootDir = process.cwd()) { 13 + const { loadNuxt, buildNuxt } = await import('@nuxt/kit') 9 14 const nuxt = await loadNuxt({ 10 15 cwd: rootDir, 11 16 dev: false, ··· 20 25 nuxt.options.modules.push(autoImportMock) 21 26 await nuxt.ready() 22 27 23 - return new Promise<{ nuxt: Nuxt, config: InlineConfig }>((resolve, reject) => { 24 - nuxt.hook('vite:extendConfig', config => { 25 - resolve({ nuxt, config }) 28 + return new Promise<GetVitestConfigOptions>((resolve, reject) => { 29 + nuxt.hook('vite:extendConfig', viteConfig => { 30 + resolve({ nuxt, viteConfig }) 26 31 throw new Error('_stop_') 27 32 }) 28 33 buildNuxt(nuxt).catch(err => { ··· 33 38 }).finally(() => nuxt.close()) 34 39 } 35 40 36 - export async function getVitestConfig(): Promise< 41 + export async function getVitestConfig(options?: GetVitestConfigOptions): Promise< 37 42 InlineConfig & { test: VitestConfig } 38 43 > { 39 - const { config: viteConfig, nuxt } = await getNuxtAndViteConfig() 44 + if (!options) 45 + options = await getNuxtAndViteConfig() 40 46 41 47 return { 42 - ...viteConfig, 48 + ...options.viteConfig, 43 49 test: { 44 50 environment: 'nuxt', 45 51 deps: { ··· 50 56 // additional deps 51 57 'vue', 52 58 'vitest-environment-nuxt', 53 - ...nuxt.options.build.transpile.filter(r => typeof r === 'string' || r instanceof RegExp) as Array<string | RegExp>, 59 + ...options.nuxt.options.build.transpile.filter(r => typeof r === 'string' || r instanceof RegExp) as Array<string | RegExp>, 54 60 ], 55 61 }, 56 62 }, 57 63 } 58 64 } 65 + 59 66 export async function defineConfigWithNuxtEnv(config: InlineConfig = {}) { 60 67 return defineConfig(async () => { 61 68 return mergeConfig(await getVitestConfig(), config)