Vue.js 如何找到elementUi';在单元测试期间使用vue test utils';什么是浅山?

Vue.js 如何找到elementUi';在单元测试期间使用vue test utils';什么是浅山?,vue.js,jestjs,element-ui,vue-test-utils,Vue.js,Jestjs,Element Ui,Vue Test Utils,在阅读了vue test utils的文档之后,我正在尝试提交 希望有人能消除我的困惑 import { mount, shallowMount } from '@vue/test-utils' import Vue from 'vue' import Form from '@/components/Form/index.vue' import ElementUI from 'element-ui' Vue.use(ElementUI) describe('Form.vue', () =>

在阅读了vue test utils的文档之后,我正在尝试提交

希望有人能消除我的困惑

import { mount, shallowMount } from '@vue/test-utils'
import Vue from 'vue'
import Form from '@/components/Form/index.vue'
import ElementUI from 'element-ui'

Vue.use(ElementUI)
describe('Form.vue', () => {

  //find it, pass
  it('mount', () => {
    const wrapper = mount(Form)
    const elButton = wrapper.find('.el-button')
    expect(elButton.isVueInstance()).toBe(true)
  })

  //can't find it, fail
  it('shallowMount', () => {
    const wrapper = shallowMount(Form)
    const elButton = wrapper.find('.el-button')
    expect(elButton.isVueInstance()).toBe(true)
  })
})
当我使用“mount”时,我可以找到elementui提供的组件

但有时我可能需要使用“shallowMount”

在这种情况下,我如何找到组件

提前谢谢




谢谢你的回答, 我找到了两种解决方法:

it('shallowMount', () => {
      const wrapper = shallowMount(Form)
      const elButton = wrapper.find(ElementUI.Button)
      expect(elButton.isVueInstance()).toBe(true)
    })

使用shallowMount时,子组件将被存根而不是渲染。这就是为什么你在第二次测试中找不到它的原因

一种选择是执行此处提到的操作:


有可能找到el按钮吗?我的意思是可能类似于
wrapper.find('el-button')
?因为我只想点击el按钮。我认为使用
mount
来呈现所有内容有点不必要。@Billyyyyyy3320如果您的问题仍然没有答案,您可以使用
wrapper.find({name:'ElButton'}).vm.$emit('click')
而不是
wrapper.find('el-button').trigger('click')cuz el按钮本身就是一个组件,并且由于其父组件(本例中的形式)的安装较浅而被截短。
it('shallowMount', () => {
      const wrapper = shallowMount(Form)
      const elButton = wrapper.find({ name: 'ElButton' })
      expect(elButton.isVueInstance()).toBe(true)
    })
  it('shallowMount', () => {
    const wrapper = shallowMount(Form)
    expect(wrapper.find(ElButtonComponent).exists()).toBe(true)
  })