我有一个导出对象的模块,我们称之为myObject。
此导出在测试用例的调用堆栈上的某个位置使用,但不直接在测试用例中使用。
我想根据测试用例模拟此导出的不同值。目前,如果我将其置于顶层并使用,则模拟可以工作jest.mock:
jest.mock('../myModulePath', () => ({myObject: {mocked: 'valueHere'}))
但是,使用jest.doMock替代方法并确保在每次测试之前重置模块
beforeEach(() => jest.resetModules())是行不通的。
我尝试在通话require后立即使用 a doMock,但正如预期的那样,它不起作用,因为我没有在测试中直接使用该模块。
有人设法实现这一目标吗?
我试图在这里搜索每个类似的问题,不幸的是没有解决方案适合我。
我目前正在尝试模拟模块中的命名导出,如下所示:
模块.ts
export function someFunctionOne() {
// implementation
}
export const someFunctionTwo = function (
) {
// implementation
}
export const someFunctionThree = () => {
// implementation
}
export const someFunctionFour = () => {
// implementation
}
Run Code Online (Sandbox Code Playgroud)
这里有四个导出,在我的测试中,我需要模拟整个模块:
测试如下所示:
模块.test.ts
import React from 'react'
import { shallow } from 'enzyme'
import Component from './component' // component I am testing
import { someFunctionOne, someFunctionTwo } from './module'
;(jest as any).mock('./module', () => ({
someFunctionOne: jest.fn(), …Run Code Online (Sandbox Code Playgroud)