如何使用Jest对Vue.js组件中的方法进行单元测试

jef*_*des 14 unit-testing vue.js jestjs vuejs2 vue-test-utils

我正在尝试对组件方法进行单元测试。这里的问题并未提出如何从单元测试中访问组件方法。

具体来说,给定下面的Vue组件,如何doSomeWork()从单元测试中访问?

Vue组件:

<template>
    <div id="ThisStuff">
        <span>
            Some other stuff is going on here
        </span>
    </div>
</template>

<script>
    import foo from 'bar'

    export default {
        props: {
            ObjectWithStuffInIt: [
                {
                    id: 1
                    bar: false
                },
                {
                    id: 2
                    bar: false
                },
            ]
        },
        data: {
            foo: "foo"
        },
        methods: {
            doSomeWork: function() {
                for (var i = 0; i < ObjectWithStuffInIt.length; i++) { 
                    if (foo === "diddly") {
                        ObjectWithStuffInIt[i].bar = true;
                    }
                }
            }
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我的测试代码:

import {createLocalVue, shallow} from 'vue-test-utils'
import ThisVueFile.test.js from '../../thisPlace/ThatPlace/ThisVueFile.vue'
import Vuex from 'vuex'

const localVue = createLocalVue()
localVue.use(Vuex);

describe('ThisVueFile.test.js', () => {
    let user;
    let store;

    beforeEach(() => {
        let getters = {
            user: () => user
        }

        store = new Vuex.Store({ getters })
    })

    // I need to fill propsData: with some local data here 
    //     because it is server data
    // I need to have access to the method
    // I need to use local data for `foo` in the test. 

    it(' When foo is set to -diddly- then set bar to true ', () => {
        foo = "diddly";
        // run the method in the component here 
        doSomeWork();

        expect(OjbectWithStuffInIt[0].bar.equals(true));
    })
})
Run Code Online (Sandbox Code Playgroud)

ton*_*y19 16

设置 propsData

使用shallowMount(或mount)创建组件包装器时,可以options使用initial 传递propsData

import MyComponent from "@/components/MyComponent";

//...

it('...', () => {
  const wrapper = shallowMount(MyComponent, {
    localVue,
    propsData: {
      myItems: [
        { id: 200, bar: false },
        { id: 300, bar: false }
      ]
    }
  });
})
Run Code Online (Sandbox Code Playgroud)

调用组件方法

包装器通过其vm属性提供对组件实例的访问,因此您可以使用以下方法直接调用该方法:

wrapper.vm.doSomeWork()
Run Code Online (Sandbox Code Playgroud)

修改组件数据属性

同样,您可以直接访问组件的数据属性:

wrapper.vm.foo = 'something'
Run Code Online (Sandbox Code Playgroud)

总的来说,您的测试可能类似于以下内容:

import { createLocalVue, shallowMount } from '@vue/test-utils'
import MyComponent from '@/components/MyComponent'

describe('MyComponent', () => {
  it('When foo is set to -something-, set bar to true', () => {
    const myItems = [
      { id: 200, bar: false },
      { id: 300, bar: false }
    ]
    const localVue = createLocalVue()
    const wrapper = shallowMount(MyComponent, {
      localVue,
      propsData: {
        myItems
      }
    })

    wrapper.vm.foo = 'something'
    wrapper.vm.doSomeWork()

    expect(myItems[0].bar).toBe(true)
  })
})
Run Code Online (Sandbox Code Playgroud)

演示 演示因 codesandbox/codesandbox-client#2058