用 vue-test-utils 和 JEST 模拟 $root

Van*_*ner 3 jestjs vue-test-utils

我的组件具有以下计算代码:

textButton() {
    const key = this.$root.feature.name === //...
    // ...
},
Run Code Online (Sandbox Code Playgroud)

现在我拼命地试图在我的测试中模拟“root”,但我不知道如何。有小费吗?

JoW*_*ter 5

Vue 测试工具为您提供了在挂载(或浅挂载)组件时注入模拟的能力。

const $root = 'some test value or jasmine spy'

let wrapper = shallow(ComponentToTest, {
  mocks: { $root }
})
Run Code Online (Sandbox Code Playgroud)

那应该很容易测试。希望有帮助