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

Commit

Permalink
fix(vitest-environment-nuxt): register imports after context is created
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 1, 2023
1 parent 867d73d commit 434b39b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/vitest-environment-nuxt/src/modules/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ export default defineNuxtModule({
nuxt.hook('imports:extend', _ => {
imports = imports.concat(_)
})
nuxt.hook('imports:sources', _ => {
nuxt.hook('imports:context', async ctx => {
// add core nuxt composables to imports
const registeredImports = await ctx.getImports()
imports = imports.concat(
// cast presets to imports
_.filter(item => nuxtImportSources.includes(item.from)).flatMap(item =>
item.imports.map(name => ({
name: name,
as: name,
from: item.from,
}))
) as Import[]
registeredImports.filter(item => nuxtImportSources.includes(item.from))
)
})
nuxt.hook('components:extend', _ => {
Expand Down Expand Up @@ -131,6 +125,7 @@ export default defineNuxtModule({
const name = call.arguments[0].value as string
const importItem = imports.find(_ => name === (_.as || _.name))
if (!importItem) {
console.log({ imports })
return this.error(`Cannot find import "${name}" to mock`)
}

Expand Down
10 changes: 10 additions & 0 deletions playground/tests/nuxt/mock-nuxt-composable-2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, it } from 'vitest'
import { mockNuxtImport } from 'vitest-environment-nuxt/utils'

mockNuxtImport('useHead', () => {
return () => true
})

it('should mock core nuxt composables', () => {
expect(useHead({})).toMatchInlineSnapshot('true')
})

0 comments on commit 434b39b

Please sign in to comment.