diff --git a/examples/app-vitest-full/tests/nuxt/mock-component-3.spec.ts b/examples/app-vitest-full/tests/nuxt/mock-component-3.spec.ts new file mode 100644 index 000000000..1a8ae21f2 --- /dev/null +++ b/examples/app-vitest-full/tests/nuxt/mock-component-3.spec.ts @@ -0,0 +1,17 @@ +import { expect, it } from 'vitest' +import { mockComponent, mountSuspended } from '@nuxt/test-utils/runtime-utils' +import { SomeComponent } from '#components' + +mockComponent('SomeComponent', async () => { + const { h } = await import('vue') + return { + setup() { + return () => h('div', null, 'Mocked') + }, + } +}) + +it('should mock', async () => { + const component = await mountSuspended(SomeComponent) + expect(component.html()).toMatchInlineSnapshot(`"
Mocked
"`) +}) diff --git a/src/module/mock.ts b/src/module/mock.ts index 8cebb0625..9793a2c8f 100644 --- a/src/module/mock.ts +++ b/src/module/mock.ts @@ -1,5 +1,5 @@ import type { Unimport } from 'unimport' -import { addVitePlugin, useNuxt } from '@nuxt/kit' +import { addVitePlugin, isIgnored, resolveIgnorePatterns, useNuxt } from '@nuxt/kit' import { createMockPlugin } from './plugins/mock' import type { MockPluginContext } from './plugins/mock' @@ -28,5 +28,13 @@ export function setupImportMocking () { ctx.components = _ }) + // We want to run Nuxt plugins on test files + nuxt.options.ignore = nuxt.options.ignore.filter(i => i !== '**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}') + if (nuxt._ignore) { + for (const pattern of resolveIgnorePatterns('**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}')) { + nuxt._ignore.add(`!${pattern}`) + } + } + addVitePlugin(createMockPlugin(ctx).vite()) }