【发布时间】:2019-09-26 22:05:39
【问题描述】:
我有一个这样的 Vue 页面:
<template>
</template>
<script>
created(){
this.doSomething();
}
methods: {
doSomething() {
.....
}
}
</script>
现在,我们要测试这个创建的钩子并检查是否调用了 doSomething() 方法。
这样试了,在package.json中也导入jest
import {
shallowMount,
createLocalVue,
} from '@vue/test-utils';
const localVue = createLocalVue();
import Xyx from '/Xyx.vue';
const init = () => {
wrapper = shallowMount(Xyx, { localVue });
cmp = wrapper.vm;
};
describe('#created', () => {
it('#doSomething', () => {
init();
wrapper.setMethods({
doSomething: jest.fn(),
})
expect(cmp.doSomething).toHaveBeenCalled();
});
});
我可以做这个创建的钩子的单元测试用例吗?
【问题讨论】:
标签: vue.js jestjs vuex vue-test-utils