Testing with vitest and testing-library is not working: it is due to using the SFC Script Setup?

4.8k views Asked by At

I'm new to Vue and especially with the composition functions. I'm trying to test a component that uses the script setup; however, it seems that it is not working.

The component is this one:

<template>
    <el-card class="box-card" body-style="padding: 38px; text-align: center;" v-loading="loading">
        <h3>Login</h3>
        <hr class="container--separator">
        <el-form ref="formRef"
            :model="form"
        >   
            <el-form-item label="Username">
                <el-input v-model="form.username" placeholder="Username"/>
            </el-form-item>
            <el-form-item label="Password">
                <el-input type="password" v-model="form.password" placeholder="Password" />
            </el-form-item>
            <el-button color="#2274A5" v-on:click="submitForm()">Login</el-button>
        </el-form>
    </el-card>
    
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import { useRouter } from 'vue-router'
import type {FormInstance} from 'element-plus'
import {useMainStore} from "../../stores/index"
import notification from "@/utils/notification"
import type User from "@/types/User"


const formRef = ref<FormInstance>()
const form: User = reactive({
  username: "",
  password: "",
}) 

const router = useRouter()
const loading = ref(false)

const submitForm = (async() => {
    const store = useMainStore()

    if (form.username === "") {
        return notification("The username is empty, please fill the field")
    }

    if (form.password === "") {
        return notification("The password is empty, please fill the field")
    }

    loading.value = true;
    await store.fetchUser(form.username, form.password);
    loading.value = false;
    
    router.push({ name: "home" })
    

}) 

</script>

<style lang="sass" scoped>
    @import "./LoginCard.scss"
</style>

When I try to test it:

import { test } from 'vitest'
import {render, fireEvent} from '@testing-library/vue'
import { useRouter } from 'vue-router'
import LoginCard from '../LoginCard/LoginCard.vue'

test('login works', async () => {     

    render(LoginCard)
    
})

I had more lines but just testing to render the component gives me this error.

TypeError: Cannot read properties of undefined (reading 'deep')
 ❯ Module.withDirectives node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:3720:17
 ❯ Proxy._sfc_render src/components/LoginCard/LoginCard.vue:53:32
     51|     loading.value = false;
     52| 
     53|     router.push({ name: "home" });

I tried to comment parts of the component to see if it was an issue with a specific line (the router for example), but the problem seems to continue. I tried to search about it but I don't know what I'm doing wrong, it is related to the component itself? Should I change how I've done the component?

1

There are 1 answers

0
mikefp On BEST ANSWER

I had the same issue, and was finally able to figure it out. Maybe this will help you.

The problem was I had to register global plugins used by my component when calling the render function.

I was trying to test a component that used a directive registered by a global plugin. In my case, it was maska, and I used the directive in a input that was rendered somewhere deeply nested inside my component, like so:

<!-- a global directive my component used -->
<input v-maska="myMask" .../>

@vue/test-utils didn't recognize it automatically, which caused the issue. To solve it, I had to pass the used plugin in a configuration parameter of the render() function:

import Maska from 'maska';

render(MyComponent, {
    global: {
        plugins: [Maska]
    }
})

Then, the issue was gone. You can find more info about render() configuration here: https://test-utils.vuejs.org/api/#global