Skip to content

Commit 6e95713

Browse files
committed
test(vitest): ignore SFC spec blocks during tests (ignore ?vue&type=spec requests)
1 parent 5de79a6 commit 6e95713

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

vitest.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ import { defineConfig } from 'vitest/config'
22
import { fileURLToPath } from 'node:url'
33
import AutoImport from 'unplugin-auto-import/vite'
44
import vue from '@vitejs/plugin-vue'
5+
import type { Plugin } from 'vite'
6+
7+
// During tests, SFC custom blocks with `lang="md"` (e.g. <spec lang="md">)
8+
// may be requested as `?vue&type=spec&index=0&lang.md` and Vite/Rollup can
9+
// attempt to parse them as JS modules, causing parse errors. Add a small
10+
// Vite plugin for the test config that returns a no-op module for those ids.
11+
function ignoreVueSpecBlocks() {
12+
return {
13+
name: 'ignore-vue-spec-blocks' as const,
14+
enforce: 'pre',
15+
transform(code: string, id: string) {
16+
try {
17+
if (id && /[?&]type=spec(?:&|$)/.test(id)) {
18+
// return an empty module so Rollup/Vite won't try to parse markdown
19+
return {
20+
code: 'export default {}',
21+
map: { mappings: '' }
22+
}
23+
}
24+
} catch {
25+
// noop
26+
}
27+
return null
28+
}
29+
} as Plugin
30+
}
531

632
export default defineConfig({
733
test: {
@@ -21,6 +47,7 @@ export default defineConfig({
2147
imports: ['vue', 'vue-i18n'],
2248
dts: false
2349
}),
50+
ignoreVueSpecBlocks(),
2451
vue()
2552
]
2653
})

0 commit comments

Comments
 (0)