I am using Vitest for Vue Unit Testing and I am currently faced with this error:
FAIL tests/unit/components/MyComponent.spec.js [ tests/unit/components/MyComponent.spec.js ]
SyntaxError: At least one <template> or <script> is required in a single file component.
My unit test is complaining that there is no <template>
and <script>
but I most certainly have those tags because I am able to display the web page and I have checked my components. The issue might be my vitest.config.js
file as shown below:
export default mergeConfig(
viteConfig,
defineConfig({
plugins: [Vue()],
test: {
globals: true,
environment: 'jsdom',
exclude: [...configDefaults.exclude, 'e2e/*', 'packages/template/*'],
root: fileURLToPath(new URL('./', import.meta.url)),
transformMode: {
web: [/\.[jt]sx$/]
}
},
root: '.'
})
)
Update
I found a temporary fix and that is to remove Vue()
in the plugins.
export default mergeConfig(
viteConfig,
defineConfig({
plugins: [], // remove Vue()
test: {
globals: true,
environment: 'jsdom',
exclude: [...configDefaults.exclude, 'e2e/*', 'packages/template/*'],
root: fileURLToPath(new URL('./', import.meta.url)),
transformMode: {
web: [/\.[jt]sx$/]
}
},
root: '.'
})
)