小编Cha*_*eau的帖子

无法在 Vue 3 setup() 中监视函数

如何使用 Jest 编写调用resetTimer并检查startTimer也被调用的测试?

代码:

setup () {
    const startTimer = () => {
        // ...
    };

    const resetTimer = () => {
        startTimer();
    };

    return {
        startTimer,
        resetTimer
    }
Run Code Online (Sandbox Code Playgroud)

测试:

import { shallowMount } from '@vue/test-utils';
import Overlay from '@/components/Overlay.vue';

const wrapper = shallowMount(Overlay);

it('resetTimer should call startTimer', () => {
    const spy = jest.spyOn(wrapper.vm, 'resetTimer');

    wrapper.vm.startTimer();
    expect(spy).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)

结果:

TypeError: object.hasOwnProperty is not a function

      187 |
      188 |     it('resetTimer should call startTimer', () => …
Run Code Online (Sandbox Code Playgroud)

vue.js jestjs spyon vuejs3 vue-composition-api

5
推荐指数
1
解决办法
533
查看次数

标签 统计

jestjs ×1

spyon ×1

vue-composition-api ×1

vue.js ×1

vuejs3 ×1