【问题标题】:Unit test method with jQuery and this.random() inside, in vue在 vue 中使用 jQuery 和 this.random() 的单元测试方法
【发布时间】:2020-04-29 08:55:35
【问题描述】:

我是单元测试的新手,我想用一个方法测试一个组件,该方法内部我使用this.random 函数和带有 Jest 的 jQuery。

我想测试是否调用了openPost方法?

当我进行测试时,它会抛出错误 this.$ is not a functionthis.random is not a function

如何编写测试?

组件:

<template>
    <div @click="openPost('note')" class="ml-3 text-center pointer noteBtn">
      <i class="fal color-9e fa-handshake"></i>
      <div class=" fs-14 color-50">metting</div>
    </div>
</template>

export default {
  data() {
    return {
      addNewKey: '',
      addNewType: '',
      showModal: false,
      showNegoModal: false,
      data: {}
    }
  },
  methods: {
    openPost(type, data) {
      this.addNewType = type
      if (data) this.data = data
      else this.data = {}
      this.addNewKey = this.random()
      this.$nextTick(() => {
        if (type === 'call') {
          this.$('#createContact').modal('show')
        } else if (type === 'note') {
          this.$('#createNote').modal('show')
        } else if (type === 'negotiation') {
          if (data) {
            this.showNegoModal = true
            this.$nextTick(() => {
              this.$('#CompanyNegoModal2').modal('show')
            })
          } else this.createnegopopup.show = true
        }
      })
    }
}

测试:

import { shallowMount } from '@vue/test-utils'
import component from '../../components/component'

global.Math.random = jest.fn()
global.$ = jest.fn()

describe('AddDetailToCompany', () => {
  let wrapper = null
  beforeEach(() => {
    wrapper = shallowMount(component)
  })
  afterEach(() => {
    wrapper.destroy()
    jest.clearAllMocks()
  })
  test('calls openPost when noteBtn is clicked', () => {
    const openPost = jest.fn()
    wrapper.find('.noteBtn').trigger('click')
    expect(openPost).toHaveBeenCalledWith('note')
    wrapper.vm.$nextTick().then(function() {
      expect(global.Math.random).toHaveBeenCalled()
      expect(global.$).toHaveBeenCalledWith('#createNote')
    })
  })
})

【问题讨论】:

    标签: unit-testing vue.js jestjs


    【解决方案1】:

    被测组件不访问全局$,而是需要模拟的实例属性:

    wrapper = shallowMount(component, { mocks: { $ } })
    

    jQuery 是可链接的需要提供可链接的模拟:

    modal = jest.fn();
    $ = jest.fn().mockReturnValue({ modal });
    

    【讨论】:

    • 我写这个:test('calls openPost when noteBtn is clicked', () =&gt; { const modal = jest.fn() const $ = jest.fn().mockReturnValue({ modal }) wrapper = shallowMount(AddDetailToCompany, { mocks: { $ } }) wrapper.find('.noteBtn').trigger('click') expect(wrapper.vm.random).not.toBe(undefined) expect(wrapper.vm.$).not.toBe(undefined) }) 但它会抛出错误this.$(...).modal is not a function
    • 如果你做的所有事情都正确,就不应该出现this.$(...).modal is not a function错误,因为它是通过mocks提供的,你可以用expect(jest.isMockFunction(wrapper.vm.$().modal)).toBe(true);进行测试。我刚刚检查了测试,它按预期工作。请提供一种方法来重现错误,以防它仍然存在。该错误可能与您没有模拟$ 的其他地方有关。顺便说一句,在原始测试中,您不会从测试中返回承诺,您将无法正确测试异步代码,请在 test 中使用 async 函数和 await wrapper.vm.$nextTick()
    猜你喜欢
    • 2019-03-20
    • 2023-01-03
    • 2019-09-03
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多