diff --git a/src/vitest-environment-nuxt/modules/mock.ts b/src/vitest-environment-nuxt/modules/mock.ts index 9f904cdbb..5d262895a 100644 --- a/src/vitest-environment-nuxt/modules/mock.ts +++ b/src/vitest-environment-nuxt/modules/mock.ts @@ -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(`})`) diff --git a/test/fixtures/nuxt-vitest/composables/use-default-export.ts b/test/fixtures/nuxt-vitest/composables/use-default-export.ts new file mode 100644 index 000000000..0bf6761c3 --- /dev/null +++ b/test/fixtures/nuxt-vitest/composables/use-default-export.ts @@ -0,0 +1 @@ +export default () => 'the original' diff --git a/test/fixtures/nuxt-vitest/tests/nuxt/mock-nuxt-composable-3.spec.ts b/test/fixtures/nuxt-vitest/tests/nuxt/mock-nuxt-composable-3.spec.ts new file mode 100644 index 000000000..87cb22633 --- /dev/null +++ b/test/fixtures/nuxt-vitest/tests/nuxt/mock-nuxt-composable-3.spec.ts @@ -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"') +})