I'm trying to setup vitest
+ @vue/test-utils
but I can't test some components that use ref
(Vue) or useCookie
(Nuxt), I have the same problem with Pinia as well where I import globally 2 functions in the nuxt.config.js like this
[
"@pinia/nuxt",
{
autoImports: ["defineStore", "acceptHMRUpdate"],
},
];
This is my vitest.config.js setup:
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import path from "path";
export default defineConfig({
plugins: [
vue(),
AutoImport({
imports: ["vue"],
dirs: ["./composables/"],
}),
],
test: {
globals: true,
environment: "jsdom",
coverage: {
provider: "c8",
reporter: "html",
reportsDirectory: "./coverage",
},
resolve: {
alias: {
"~": path.resolve(__dirname, "./"),
},
},
},
});
and the component I'm trying to test is something like this:
<script setup>
import AButton from '~/components/atoms/a-button/a-button.vue'
...
defineProps({
label: {
type: String,
required: true
},
})
const show = ref(false)
const Squad = useCookie('squad', { maxAge: 60 * 60 * 24 * 7 })
const appStore = useAppStore()
In Nuxt 3 as you know you don't import { ref } from vue
or ... useCookie from..
they are auto-imported but vitest
doesn't recognize them, I saw a couple of questions but none have an answer that solves my problem.
These are some: