【问题标题】:Why is wrapper.findComponent not working for @vue/test-utils为什么 wrapper.findComponent 不适用于 @vue/test-utils
【发布时间】:2021-12-28 16:33:51
【问题描述】:

当我尝试使用wrapper.findComponent 时,我收到以下错误:

ErrorWrapper { 选择器: { ref: 'loginTab' } }

我在网上搜索过,但无济于事。这是我的测试代码:

describe('Sign-Up Test Cases', () => {
    const wrapper = mount(SignUp, {
        methods: {
            getProv: jest.fn()
        },
        localVue
    })
    
    it('Dialog contains a Login Input Field', () => {
        console.log(wrapper.findComponent({ ref: 'loginTab' }))
        expect(wrapper.findComponent({ ref: 'loginTab' }))
    })
})

localVue 已定义。 检查我使用findComponent 函数的it 语句。

更新:我尝试了其他方法。

我试过了,没用。

import Vuetify, {VTab} from 'vuetify/lib'
// Some Code here
console.log(wrapper.findComponent(VTab))

它没有工作。我收到以下错误:

ErrorWrapper {selector: [Function: VueComponent]}

【问题讨论】:

    标签: vue.js unit-testing jestjs vue-test-utils


    【解决方案1】:

    您必须导入组件并将其用作findComponent 方法的有效负载:

    const VTab = {
       template: '<div><slot></div>
    }
    
    const wrapper = shallowMount(SignUp, {
            global: {
               stubs: { VTab }
            },
        })
    
    wrapper.findComponent(VTab)
    

    【讨论】:

    • 我试过了,但没有用。 import Vuetify, {VTab} from 'vuetify/lib' console.log(wrapper.findComponent(VTab)) 没有用。我收到以下错误:ErrorWrapper {selector: [Function: VueComponent]}
    • 如果它是来自 vuetify 的组件,则必须对组件进行存根。我改变我的答案
    猜你喜欢
    • 2021-11-08
    • 2021-04-16
    • 2019-10-24
    • 2019-03-28
    • 2019-02-10
    • 2021-11-13
    • 2019-02-15
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多