vue-test-utils 为 document.querySelector 返回 null

Dav*_*Hut 2 javascript vue.js vue-test-utils

我正在为 Vue.js 组件编写单元测试,该组件显示一些单选按钮,并在选中按钮时发出带有单选按钮 id 的事件:

应用程序元素.vue:

<template>
  <div id="element">
    <label>
      <input
        type="radio"
        name="Element"
        id="element1"
        v-on:change="chooseElement"
      />
      Element1
    </label>
    <label>
      <input
        type="radio"
        name="Element"
        id="element2"
        v-on:change="chooseElement"
      />
      Element2
    </label>
    <label>
      <input
        type="radio"
        name="Element"
        id="element3"
        v-on:change="chooseElement"
      />
      Element3
    </label>
    <label>
      <input
        type="radio"
        name="Element"
        id="element4"
        v-on:change="chooseElement"
      />
      Element4
    </label>
  </div>
</template>

<script>
export default {
  methods: {
    chooseElement () {
      var chosenElement = document.querySelector('input[name="Element"]:checked').id
      this.$emit('passChosenElement', {category: chosenElement})
    }
  }
}
</script>

<style>
</style>
Run Code Online (Sandbox Code Playgroud)

我为该组件编写了以下测试:

ApplicationElement.spec.js:

import { shallowMount, createLocalVue } from '@vue/test-utils'
import 'regenerator-runtime/runtime'
import ApplicationElement from '@/components/ApplicationElement.vue'

const localVue = createLocalVue()

describe('ApplicationElement tests', () => {
    it('should emit event with element1 parameter on corresponding radio button choice', async() => {
        const wrapper = shallowMount(ApplicationElement, {
            localVue,
            attachTo: document.body,
            propsData: {
                recursionNumber: 1
            }
        })
        await wrapper.find('input[type="radio"][id="element1"]').setChecked()
        expect(wrapper.emitted().passChosenElement[0]).toEqual([{element: 'element1'}])
        wrapper.destroy()
    })
})
Run Code Online (Sandbox Code Playgroud)

测试失败并显示:“TypeError: Cannot read property 'id' of null” 如果我从组件中的 ChooseElement 方法中删除document.querySelector行并手动发出正确的 id,则测试会成功。

如何让document.querySelector与 vue-test-utils 一起使用?

tja*_*rbo 5

我知道,这个问题已经有点老了,但我终于在这里找到了解决方案。您需要将组件attachTo(自 2021 年 5 月起)附加到 html 元素。解决方案如下所示:

describe('ApplicationElement tests', () => {
  it('should emit event with element1 parameter on corresponding radio button choice', async() => {
      const elem = document.createElement('div')
      if (document.body) {
         document.body.appendChild(elem)
      }

      const wrapper = shallowMount(ApplicationElement, {
          localVue,
          propsData: {
              recursionNumber: 1
          },
          attachTo: elem,
      })
      await wrapper.find('input[type="radio"][id="element1"]').setChecked()
      expect(wrapper.emitted().passChosenElement[0]).toEqual([{element: 'element1'}])
      wrapper.destroy()
  })
});
   
Run Code Online (Sandbox Code Playgroud)

wrapper.destroy()正如您已经所做的那样,每次测试后致电很重要。重构小技巧:放入回调函数wrapper.destroy()afterEach。在这里您可以找到有关attachTo的更多信息。