0

再会,

我在以下 Vue.JS 单元测试中遇到了困难,其中没有调用模拟的导入函数 (catchException)。

我曾尝试使用“jest.mock”和“jest.spyon”方法,但结果相同。

功能:

import { catchException } from '@/exceptionHandler/index'
logout () {
            this.loginLoading = true
            this.logout({
                //logout user
            }).then(() => {
                //do something
            }).catch(error => {
                catchException({
                    error
                })
            })
        }

Jest 单元测试:

import { catchException } from '@/exceptionHandler/index'

jest.mock('@/exceptionHandler/index', () => ({
    ...(jest.requireActual('@/exceptionHandler/index')),
    catchException: jest.fn()
}))


 beforeEach(function () {
        actions = {
            LOGOUT: jest.fn()
        }
        store = new Vuex.Store({
            modules: {
                user: {
                    namespaced: true,
                    actions 
                }
            }
        })
            wrapper = shallowMount(logout, {
            localVue,
            router,
            store,
        })
    })

 describe('should call exception function', function () {
        it('should call the exception function', async function () {
            actions.LOGOUT.mockReturnValueOnce(Promise.reject({
                    error: "random error"
            }))
            await wrapper.vm.logout()
            expect(catchException).toBeCalled()
        })
    })
})

出现以下错误:

expect(jest.fn()).toBeCalled()
预期呼叫数:>= 1 已接呼叫数:0

如果我删除模拟,该函数会被调用,但会出现以下错误:

匹配器错误:接收到的值必须是模拟或间谍函数

Received has type:  function
Received has value: [Function catchException]
4

0 回答 0