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

Commit

Permalink
feat: register all auto-imports for mocking (#254)
Browse files Browse the repository at this point in the history
Co-authored-by: Ghazi <ghazialhouwari@gmail.com>
  • Loading branch information
aapokiiso and ghazialhouwari committed Jul 17, 2023
1 parent fdc4263 commit 8799e6a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
17 changes: 6 additions & 11 deletions packages/vitest-environment-nuxt/src/modules/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Import } from 'unimport'
import type { Import, Unimport } from 'unimport'
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
import { walk } from 'estree-walker'
import type { CallExpression } from 'estree'
Expand All @@ -25,8 +25,6 @@ export interface MockComponentInfo {
factory: string
}

const nuxtImportSources = ['#app', '#vue-router', 'vue-demi', '@unhead/vue']

/**
* This module is a macro that transforms `mockNuxtImport()` to `vi.mock()`,
* which make it possible to mock Nuxt imports.
Expand All @@ -36,22 +34,19 @@ export default defineNuxtModule({
name: PLUGIN_NAME,
},
setup(_, nuxt) {
let importsCtx: Unimport
let imports: Import[] = []
let components: Component[] = []

nuxt.hook('imports:extend', _ => {
imports = imports.concat(_)
})
nuxt.hook('imports:context', async ctx => {
// add core nuxt composables to imports
const registeredImports = await ctx.getImports()
imports = imports.concat(
registeredImports.filter(item => nuxtImportSources.includes(item.from))
)
importsCtx = ctx
})
nuxt.hook('components:extend', _ => {
components = _
})
nuxt.hook('ready', async () => {
imports = await importsCtx.getImports()
})

// Polyfill Array.prototype.findLastIndex for legacy Node.js
function findLastIndex<T>(arr: T[], predicate: (item: T) => boolean) {
Expand Down
17 changes: 15 additions & 2 deletions playground/modules/custom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { defineNuxtModule } from '@nuxt/kit'
import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit'

export default defineNuxtModule({
meta: {
name: 'custom',
},
setup(_, _nuxt) {
console.log('From custom module!')
},

const { resolve } = createResolver(import.meta.url)

addImports([
{
name: 'useCustomModuleAutoImportedTarget',
from: resolve('runtime/composables/auto-import-mock')
},
{
name: 'useCustomModuleAutoImportedNonTarget',
from: resolve('runtime/composables/auto-import-mock')
}
])
}
})
7 changes: 7 additions & 0 deletions playground/modules/runtime/composables/auto-import-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function useCustomModuleAutoImportedTarget() {
return 'the original'
}

export function useCustomModuleAutoImportedNonTarget() {
return 'the original'
}
9 changes: 9 additions & 0 deletions playground/tests/nuxt/auto-import-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ mockNuxtImport<typeof useAutoImportedTarget>('useAutoImportedTarget', () => {
return () => 'mocked!'
})

mockNuxtImport<typeof useCustomModuleAutoImportedTarget>('useCustomModuleAutoImportedTarget', () => {
return () => 'mocked!'
})

it('should mock', () => {
vi.fn()
expect(useAutoImportedTarget()).toMatchInlineSnapshot('"mocked!"')
expect(useAutoImportedNonTarget()).toMatchInlineSnapshot('"the original"')
})

it('should mock composable from external package', () => {
expect(useCustomModuleAutoImportedTarget()).toMatchInlineSnapshot('"mocked!"')
expect(useCustomModuleAutoImportedNonTarget()).toMatchInlineSnapshot('"the original"')
})

0 comments on commit 8799e6a

Please sign in to comment.