Skip to content

Commit

Permalink
fix: handle mocking composables that use default export
Browse files Browse the repository at this point in the history
  • Loading branch information
niko-chaffinchicas committed Sep 12, 2023
1 parent e98d71e commit cdff550
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/vitest-environment-nuxt/modules/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,19 @@ export default defineNuxtModule({
)}) } }`,
]
for (const mock of mocks) {
lines.push(
` mocks[${JSON.stringify(from)}][${JSON.stringify(
mock.name
)}] = await (${mock.factory})()`
)
if (mock.import.name === 'default') {
lines.push(
` mocks[${JSON.stringify(from)}]["default"] = await (${
mock.factory
})()`
)
} else {
lines.push(
` mocks[${JSON.stringify(from)}][${JSON.stringify(
mock.name
)}] = await (${mock.factory})()`
)
}
}
lines.push(` return mocks[${JSON.stringify(from)}] `)
lines.push(`})`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'the original'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, it } from 'vitest'
import { mockNuxtImport } from 'vitest-environment-nuxt/utils'

mockNuxtImport('useAutoImportedTarget', () => {
return () => 'mocked'
})

mockNuxtImport('useDefaultExport', () => {
return () => 'mocked'
})

it('should mock composables from project', () => {
expect(useAutoImportedTarget()).toMatchInlineSnapshot('"mocked"')
})

it('should mock composables from project even if it uses `export default`', () => {
expect(useDefaultExport()).toMatchInlineSnapshot('"mocked"')
})

0 comments on commit cdff550

Please sign in to comment.