用vitest和Testing-ubrary进行测试不起作用:这是由于使用SFC脚本设置吗?

发布于 2025-01-24 12:22:20 字数 2644 浏览 0 评论 0原文

我是VUE的新手,尤其是构图功能。我正在尝试测试使用脚本设置的组件;但是,看来它不起作用。

组件是一个:

<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>

当我尝试测试它时:

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)
    
})

我有更多的行,但只是测试以渲染组件会给我这个错误。

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'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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人迟暮 2025-01-31 12:22:20

我遇到了同样的问题,终于能够弄清楚了。也许这会帮助您。

问题是,我必须在调用渲染函数时注册组件使用的全局插件。

我正在尝试测试使用全局插件注册的指令的组件。就我而言,它是Maska,我在一个深深嵌套在组件内的输入中使用了指令,就像这样:

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

@vue/test-utils无法自动识别它,这引起了问题。为了解决它,我必须在Render()函数的配置参数中传递使用的插件:

import Maska from 'maska';

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

然后,问题已经消失。您可以找到有关Render()的更多信息
配置在这里:
https://test-utils.vuejs.org/api/papi/#global

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文