vue+jest+vue-test-utils 单元测试
npm install --save-dev jest @vue/test-utils
//package.json
"scripts": {
"test": "jest",
}
npm install --save-dev vue-jest
const path = require('path'); module.exports = { verbose: true, testURL: 'http://localhost/', rootDir: path.resolve(__dirname, '../../../'), moduleFileExtensions: [ 'js', 'json', 'vue', ], testMatch: [ // 匹配测试用例的文件 '<rootDir>/src/test/unit/specs/*.spec.js', ], transform: { '^.+\\.js$': 'babel-jest', '.*\\.(vue)$': 'vue-jest', }, testPathIgnorePatterns: [ '<rootDir>/test/e2e', ], moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1', }, };
//ATest.vue组件 <template> <div> <Checkbox class="checkAlls" @on-change="checkboxChange" v-model="checkAll">全选</Checkbox> </div> </template> <script> export default { name: 'ATest', data() { return { checkAll: false, }; }, methods: { checkboxChange(value) { this.$emit('on-change', value); }, }, }; </script>
import { mount, createLocalVue } from '@vue/test-utils';
import ATest from '@/components/ATest.vue';
import iviewUI from 'view-design';
const localVue = createLocalVue();
localVue.use(iviewUI);
describe('ATest.vue',()=>{
const wrapper =mount(ATest, {
localVue
});
it("事件被正常触发",()=>{
const stub = jest.fn();
wrapper.setMethods({ checkboxChange: stub });
//触发自定义事件
wrapper.find(".checkAlls").vm.$emit("on-change");
wrapper.setData({checkAll: true});
expect(wrapper.vm.checkAll).toBe(true);
})
})
-
mount()
-
shallowMount()
//App.vue <template> <div id="app"> <Page :messages="messages"></Page> </div> </template>
//子组件 <template> <div> <p v-for="message in messages" :key="message">{{message}}</p> </div> </template>
//测试用例App.spec.js
import { mount } from 'vue-test-utils';
import App from '@/App';
describe('App.test.js', () => {
let wrapper;
let vm;
beforeEach(() => {
wrapper = mount(App);
vm = wrapper.vm;
wrapper.setProps({ messages: ['Cat'] })
});
// 为App的单元测试增加快照(snapshot):
it('has the expected html structure', () => {
expect(vm.$el).toMatchSnapshot()
})
});
exports[`App.test.js has the expected html structure 1`] =
` <div id="app" > <div> <p> Cat </p> </div> </div> `;
exports[`App.test.js has the expected html structure 1`] =
` <div id="app" > <!----> </div> `;
-
createLocalVue
import { createLocalVue, shallowMount } from '@vue/test-utils'import Foo from './Foo.vue'
const localVue = createLocalVue()const wrapper = shallowMount(Foo, {
localVue,
mocks: { foo: true }})expect(wrapper.vm.foo).toBe(true)
const freshWrapper = shallowMount(Foo)expect(freshWrapper.vm.foo).toBe(false)
-
选择器 (详细看Vue-Test-Utils官网介绍)
-
$route 和 $router
import { mount} from '@vue/test-utils'
import Test from '@/components/common/Test.vue';
describe('Test.vue',()=>{
const wrapper = mount(Test, {
mocks: {
$route: { path: '/login' }
}
})
it("test", ()=>{
expect(wrapper.vm.$route.path).toMatch('/login')
})
})
-
状态管理Vuex
import Vuex from 'vuex';
import {mount, createLocalVue} from '@vue/test-utils';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('Test.vue',()=>{
let wrapper;
let getters;
let store;
let action;
let mutations;
let option;
beforeEach(()=>{
state = {
name: '张三'
}
getters = {
GET_NAME: ()=> '张三'
}
mutations = {
SET_NAME: jest.fn()
}
action = {
setName: jest.fn()
}
store = new Vuex.Store({
getters,
mutations,
action
})
option = {
store,
localVue
}
wrapper = mount(Test, option)
})
it("测试vuex", ()=>{
expect(getters.GET_Name()).toEqual('张三');
wrapper.vm.$store.state.name= "李四";
expect(wrapper.vm.name).toMatch('李四');
wrapper.find('.btn').trigger('click')
expect(actions.setName).toHaveBeenCalled()
wrapper.find({ref: 'testComp'}).vm.$emit('on-select');
expect(mutations.SET_NAME).toBeCalled()
})
})
-
wrapper
-
beforeEach(fn) 在每一个测试之前需要做的事情,比如测试之前将某个数据恢复到初始状态
-
afterEach(fn) 在每一个测试用例执行结束之后运行
-
beforeAll(fn) 在所有的测试之前需要做什么
-
afterAll(fn) 在测试用例执行结束之后运行
//Test.vue
mounted() {
this.getDataList();
},
methods: {
getDataList() {
this.$api.data.getData().then((res) => {
this.List= res.data;
});
}
import {shallowMount, createLocalVue} from '@vue/test-utils';
const localVue = createLocalVue();
//axios请求
jest.mock('../data.js', ()=>({
getData: () => Promise.resolve({data:{name:'张三'}})
}))
describe('Test.vue',()=>{
const option;
let wrapper =shallowMount(Test, option)
it("异步接口被正常执行", async()=>{
const getDataList= jest.fn();
option.methods = {getDataList};
shallowMount(Test, option);
await expect(getDataList).toBeCalled();
})
it('测试异步接口的返回值', () => {
return localVue.nextTick().then(() => {
expect(wrapper.vm.List).toEqual( {name:'张三'});
})
})
})
[vue-test-utils]: find did not return .btn, cannot call trigger() on empty Wrapper
//出现该问题的时候,除了要确保你的组件确实存在该类名的情况,还要确保存在v-if的时候是否为true,如果为false,只需要在单测里将其设置为true,该问题便可解决
"ReferenceError: sessionStorage is not defined"
//出现该问题只需要去模拟本地存储就可以了,npm提供了一个模拟本地存储数据的依赖包mock-local-storage,安装后在单测文件里导入即可
//import 'mock-local-storage';
//包地址:https://www.npmjs.com/package/mock-local-storage
TypeError: Cannot read property xxxx of undefined
//这个问题的一般解决方法是直接mock数据
-
%stmts是语句覆盖率(statement coverage):是否每个语句都执行了?
-
%Branch分支覆盖率(branch coverage):是否每个if代码块都执行了?
-
%Funcs函数覆盖率(function coverage):是否每个函数都调用了?
-
%Lines行覆盖率(line coverage):是否每一行都执行了?
- npx jest –init 生成配置文件jest.config.js
- package.json添加上
--coverage
这个参数
//修改package.json
"scripts": {
"test": "jest --coverage"
}
npm run test之后会生成coverage文件
然后再网页打开index.html,就会看到下图
三种颜色分别代表不同比例的覆盖率(<50%红色,50%~80%灰色, ≥80%绿色)
点击文件名可以查看代码的执行情况,
旁边显示的1x代表执行的次数