在单元测试Vue组件时无法模拟$ root。$ refs

时间:2019-05-16 13:18:40

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

我有一个组件,其中im在其挂接的钩子中使用$ root的引用。

//here's mounted of my Vue component
mounted() {
    this.$root.$refs.myRef.show()
}

这是测试用例

it('test mount', () => {
  shallowMount(myComponent)
})

但是它没有得到根的引用

//error
Could notresolve show() of undefined

我也尝试过模拟$ root

it('test mount', () => {
  shallowMount(myComponent, {
    mocks: {
      $root: {
        $refs: {
          myRef: {
            show: jest.fn()
          }
        }
      }
    }
  })
})

但是仍然会出现相同的错误。

0 个答案:

没有答案