[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.

fix: move nuxt overrides to `environmentOptions`

Daniel Roe (May 29, 2023, 4:39 AM -0500) 2cc5038d 914cee4b

+38 -14
+5 -3
playground/vitest.config.ts
··· 7 7 coverage: { 8 8 reportsDirectory: 'coverage', 9 9 }, 10 + environmentOptions: { 11 + nuxt: { 12 + rootDir: fileURLToPath(new URL('./', import.meta.url)), 13 + }, 14 + }, 10 15 }, 11 - nuxt: { 12 - rootDir: fileURLToPath(new URL('./', import.meta.url)), 13 - } 14 16 })
+32 -10
packages/nuxt-vitest/src/config.ts
··· 11 11 } 12 12 13 13 // https://github.com/nuxt/framework/issues/6496 14 - async function startNuxtAndGetViteConfig (rootDir = process.cwd(), overrides?: Partial<NuxtConfig>) { 14 + async function startNuxtAndGetViteConfig( 15 + rootDir = process.cwd(), 16 + overrides?: Partial<NuxtConfig> 17 + ) { 15 18 const { loadNuxt, buildNuxt } = await import('@nuxt/kit') 16 19 const nuxt = await loadNuxt({ 17 20 cwd: rootDir, 18 21 dev: false, 19 - overrides: defu({ 20 - ssr: false, 21 - app: { 22 - rootId: 'nuxt-test', 22 + overrides: defu( 23 + { 24 + ssr: false, 25 + app: { 26 + rootId: 'nuxt-test', 27 + }, 23 28 }, 24 - }, overrides), 29 + overrides 30 + ), 25 31 }) 26 32 27 33 if ( ··· 54 60 'vite:vue-jsx': [viteJsxPlugin, 'vueJsx'], 55 61 } as const 56 62 57 - export async function getVitestConfigFromNuxt ( 63 + export async function getVitestConfigFromNuxt( 58 64 options?: GetVitestConfigOptions, 59 65 overrides?: NuxtConfig 60 66 ): Promise<InlineConfig & { test: VitestConfig }> { ··· 86 92 { 87 93 name: 'disable-auto-execute', 88 94 enforce: 'pre', 89 - transform (code, id) { 95 + transform(code, id) { 90 96 if (id.match(/nuxt3?\/.*\/entry\./)) { 91 97 return code.replace( 92 98 /(?<!vueAppPromise = )entry\(\)\.catch/, ··· 131 137 } 132 138 } 133 139 134 - export function defineVitestConfig (config: InlineConfig & { nuxt?: Partial<NuxtConfig> } = {}) { 140 + export function defineVitestConfig(config: InlineConfig = {}) { 135 141 return defineConfig(async () => { 136 142 // When Nuxt module calls `startVitest`, we don't need to call `getVitestConfigFromNuxt` again 137 143 if (process.env.__NUXT_VITEST_RESOLVED__) return config 138 - return mergeConfig(await getVitestConfigFromNuxt(undefined, config.nuxt), config) 144 + 145 + const overrides = config.test?.environmentOptions?.nuxt?.overrides || {} 146 + overrides.rootDir = config.test?.environmentOptions?.nuxt?.rootDir 147 + 148 + return mergeConfig( 149 + await getVitestConfigFromNuxt(undefined, overrides), 150 + config 151 + ) 139 152 }) 153 + } 154 + 155 + declare module 'vitest' { 156 + interface EnvironmentOptions { 157 + nuxt?: { 158 + rootDir?: string 159 + overrides?: NuxtConfig 160 + } 161 + } 140 162 }
+1 -1
packages/vitest-environment-nuxt/src/runtime/mount.ts
··· 15 15 16 16 export async function mountSuspended< 17 17 T extends DefineComponent<any, any, any, any> 18 - > (component: T, options?: MountSuspendedOptions) { 18 + >(component: T, options?: MountSuspendedOptions) { 19 19 const { 20 20 props = {}, 21 21 attrs = {},