VUE测试UTILS:如何将Vuelidate验证规则传递给儿童组件?

发布于 2025-01-27 22:38:53 字数 962 浏览 5 评论 0 原文

在尝试通过使用 vue test utils ,测试儿童组件和其他内容之间的互动来编写组件测试时,由于儿童组件中的Vuelidate使用,我被卡住了。以下是一个简化的示例:

// parent component code

<template>
   <div>
      <childA />
   </div>
</template>
//childA code

<template>
   <input v-model="value" />
</template>

<script>
   ...
   validations: {
      value: {
         required
      }
   }
...
</script>
// parent component test
...
const wrapper = mount(MyParentComponent, {
   ...,
   components: {
      childA,
   },
   validations: {
      value: required
   },
   ...
})

我试图找到可以安装的解决方案(请注意,我也想安装子组件,因此 shack> shallow-mount 不是我想要的)儿童组成部分,具有各自的Vuelixed验证规则,但我仍然没有找到任何解决方案。

取而代之的是,我的测试给了我类似的错误:

Cannot read property `value` of undefined

这很有意义,因为该测试无法访问子组件的 $ v 实例。

到目前为止有人取得了成就吗?

while trying to write a component test by using vue test utils, testing interaction between child components and stuff, I am stuck due to usage of Vuelidate from child components. Below is an example simplified:

// parent component code

<template>
   <div>
      <childA />
   </div>
</template>
//childA code

<template>
   <input v-model="value" />
</template>

<script>
   ...
   validations: {
      value: {
         required
      }
   }
...
</script>
// parent component test
...
const wrapper = mount(MyParentComponent, {
   ...,
   components: {
      childA,
   },
   validations: {
      value: required
   },
   ...
})

I have tried to find a solution out there that I could mount (note here that I WANT to mount also the child components, so shallow-mount is not what I look for) the child component, with it's respective Vuelidate validation rules, but I still haven't found any solution.

Instead, my test gives me errors like:

Cannot read property `value` of undefined

which makes sense, since the test cannot access the child component's $v instance.

Has anyone achieved it so far?

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

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

发布评论

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

评论(1

暗喜 2025-02-03 22:38:53

要回答您的问题,并且在进行了一些测试之后,我相信您错过了数据 Mount

  1. mount :渲染子组件
  2. shallowmount :不会渲染子组件

myParentComponent 需要在选项中具有您的子女组件的结构,所以这就是为什么他正在返回错误

,我看到您'重新传递组件的导入,但不要忘记您的测试文件夹不在您的 src 文件夹

从“@/components/childa”;

不将不会相反,我建议直接使用绝对路径来导入您的孩子组件或使用配置来解决它们

    const wrapper = mount(MyParentComponent, {
      data() {
        return {
          value: null
        }
      },
      components: {
        ChildA: () => import('../../src/components/ChildA'),
      },
      validations: {
        value: required
      },
    })

For answering your question and after i've did some test i believe you missed the data part inside your mount

  1. mount: render child components
  2. shallowMount: doesn't render child components

MyParentComponent need to have in the options the structure of you're child component so this is why he is returning the error

And i saw that you're passing the import of your component directly but don't forget that your test folder is outside of your src folder

import ChildA from "@/components/ChildA";

will not work instead i propose to use absolute path directly to import your child component or use a configuration to resolve them

    const wrapper = mount(MyParentComponent, {
      data() {
        return {
          value: null
        }
      },
      components: {
        ChildA: () => import('../../src/components/ChildA'),
      },
      validations: {
        value: required
      },
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文