Utilities for testing Vue components
@eddyerburgh
github.com/eddyerburgh
medium.com/@edward.yerburgh
npm install --save-dev vue-test-utils
const wrapper = mount(Component)
wrapper.vm.someMethod()
wrapper.html()
wrapper.setProps({
propA: 'value'
})
import { mount } from 'vue-test-utils'
import Component from '~/components/Component'
test('increments counter', () => {
const wrapper = mount(Component)
wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('1')
})
vue-test-utils.vuejs.org
mount
shallow
import { shallow } from 'vue-test-utils'
import Component from '~/components/Component'
import ChildComponent from '~/components/ChildComponent'
test('passes aProp to ChildComponent', () => {
const wrapper = shallow(Component)
const cWrapper = wrapper.find(ChildComponent)
expect(cWrapper.hasProp('aProp', 'value')).toBe(true)
})
import { shallow, createLocalVue } from 'vue-test-utils'
import Router from 'vue-router'
import Component from '~/components/Component'
const localVue = createLocalVue()
localVue.use(Router)
const wrapper = shallow(Component, {
localVue
})
wrapper.vm.$route // { path : '/' }
const freshWrapper = shallow(Component)
freshWrapper.vm.$route // undefined