根据 jest 文档,使用 genMockFromModule
是个好主意,然后根据您的需要模拟特定方法:https://jestjs.io/docs/en/jest-object#jestgenmockfrommodulemodulename
这适用于默认导出,但如何将其用于命名导出?
我试过这样做:
const matches = jest.genMockFromModule("../matches");
matches.index = jest.fn();
export {
...matches
}
但是导出方法时不能使用展开运算符。
这样做:
const matches = jest.genMockFromModule("../matches");
matches.index = jest.fn();
export matches
也不起作用,因为它需要一个声明......
我错过了什么吗?
编辑:添加 matches.js 示例代码
matches.js
中的代码示例(我试图模拟的文件):
import { dwf_api_endpoint } from "../config/api";
import Client from "./Client";
export const index = (): Promise<Object> =>
Client.get(`${dwf_api_endpoint}/matches`);
export const show = (id: number, ws: boolean): Promise<Object> =>
Client.get(`${dwf_api_endpoint}/matches/${id}${ws ? "?ws=1" : ""}`);
最佳答案
原来我只需要使用module.exports
:
const matches = jest.genMockFromModule("../matches.js");
matches.index = jest.fn();
module.exports = matches;
关于javascript - Jest genMockFromModule 名为 exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56422230/