小编Nee*_*dra的帖子

使用 `vue-test-utils` 和 `jest` 使用 `Created` 钩子进行测试

我有一个像这样的 Vue 页面:

<template>
</template>

<script>
created(){
    this.doSomething();
}

methods: {
    doSomething() {
        .....
    }
}

</script>
Run Code Online (Sandbox Code Playgroud)

现在,我们要测试这个创建的钩子并检查 doSomething() 方法是否被调用。

这样试过,jest也是在package.json中导入的

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();
  });
});
Run Code Online (Sandbox Code Playgroud)

我可以做这个创建的钩子的单元测试用例吗?

vue.js jestjs vuex vue-test-utils

11
推荐指数
2
解决办法
8449
查看次数

标签 统计

jestjs ×1

vue-test-utils ×1

vue.js ×1

vuex ×1