[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: register all auto-imports for mocking (#254)

Co-authored-by: Ghazi <ghazialhouwari@gmail.com>

Aapo Kiiso (Jul 17, 2023, 2:42 PM +0100) 8799e6a8 fdc4263b

+37 -13
+15 -2
playground/modules/custom.ts
··· 1 - import { defineNuxtModule } from '@nuxt/kit' 1 + import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit' 2 2 3 3 export default defineNuxtModule({ 4 4 meta: { ··· 6 6 }, 7 7 setup(_, _nuxt) { 8 8 console.log('From custom module!') 9 - }, 9 + 10 + const { resolve } = createResolver(import.meta.url) 11 + 12 + addImports([ 13 + { 14 + name: 'useCustomModuleAutoImportedTarget', 15 + from: resolve('runtime/composables/auto-import-mock') 16 + }, 17 + { 18 + name: 'useCustomModuleAutoImportedNonTarget', 19 + from: resolve('runtime/composables/auto-import-mock') 20 + } 21 + ]) 22 + } 10 23 })
+9
playground/tests/nuxt/auto-import-mock.spec.ts
··· 5 5 return () => 'mocked!' 6 6 }) 7 7 8 + mockNuxtImport<typeof useCustomModuleAutoImportedTarget>('useCustomModuleAutoImportedTarget', () => { 9 + return () => 'mocked!' 10 + }) 11 + 8 12 it('should mock', () => { 9 13 vi.fn() 10 14 expect(useAutoImportedTarget()).toMatchInlineSnapshot('"mocked!"') 11 15 expect(useAutoImportedNonTarget()).toMatchInlineSnapshot('"the original"') 16 + }) 17 + 18 + it('should mock composable from external package', () => { 19 + expect(useCustomModuleAutoImportedTarget()).toMatchInlineSnapshot('"mocked!"') 20 + expect(useCustomModuleAutoImportedNonTarget()).toMatchInlineSnapshot('"the original"') 12 21 })
+6 -11
packages/vitest-environment-nuxt/src/modules/mock.ts
··· 1 - import type { Import } from 'unimport' 1 + import type { Import, Unimport } from 'unimport' 2 2 import { addVitePlugin, defineNuxtModule } from '@nuxt/kit' 3 3 import { walk } from 'estree-walker' 4 4 import type { CallExpression } from 'estree' ··· 25 25 factory: string 26 26 } 27 27 28 - const nuxtImportSources = ['#app', '#vue-router', 'vue-demi', '@unhead/vue'] 29 - 30 28 /** 31 29 * This module is a macro that transforms `mockNuxtImport()` to `vi.mock()`, 32 30 * which make it possible to mock Nuxt imports. ··· 36 34 name: PLUGIN_NAME, 37 35 }, 38 36 setup(_, nuxt) { 37 + let importsCtx: Unimport 39 38 let imports: Import[] = [] 40 39 let components: Component[] = [] 41 40 42 - nuxt.hook('imports:extend', _ => { 43 - imports = imports.concat(_) 44 - }) 45 41 nuxt.hook('imports:context', async ctx => { 46 - // add core nuxt composables to imports 47 - const registeredImports = await ctx.getImports() 48 - imports = imports.concat( 49 - registeredImports.filter(item => nuxtImportSources.includes(item.from)) 50 - ) 42 + importsCtx = ctx 51 43 }) 52 44 nuxt.hook('components:extend', _ => { 53 45 components = _ 46 + }) 47 + nuxt.hook('ready', async () => { 48 + imports = await importsCtx.getImports() 54 49 }) 55 50 56 51 // Polyfill Array.prototype.findLastIndex for legacy Node.js
+7
playground/modules/runtime/composables/auto-import-mock.ts
··· 1 + export function useCustomModuleAutoImportedTarget() { 2 + return 'the original' 3 + } 4 + 5 + export function useCustomModuleAutoImportedNonTarget() { 6 + return 'the original' 7 + }