Skip to content

Commit

Permalink
fix: allow mocking more nuxt auto-imports
Browse files Browse the repository at this point in the history
resolves #153
  • Loading branch information
danielroe committed May 28, 2023
1 parent 5f3cde3 commit eb45d62
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/vitest-environment-nuxt/modules/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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 @@ -43,16 +45,12 @@ export default defineNuxtModule({
// add core nuxt composables to imports
imports = imports.concat(
// cast presets to imports
_.filter(item => item.from === '#app').flatMap(item =>
item.imports.flatMap(name => {
return name.toString().startsWith('use')
? {
name: name,
as: name,
from: item.from,
}
: []
})
_.filter(item => nuxtImportSources.includes(item.from)).flatMap(item =>
item.imports.map(name => ({
name: name,
as: name,
from: item.from,
}))
) as Import[]
)
})
Expand Down

0 comments on commit eb45d62

Please sign in to comment.