javascript - Jest genMockFromModule 名为 exports

标签 javascript jestjs

根据 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/

相关文章:

javascript - ShrinkSafe 缩小工具提示 : debugging Javascript code via JSLint: style or real errors?

javascript - firefox中输入框取数值

unit-testing - axios 捕获错误请求失败,状态码为 404

typescript - 如何使用 Apollo Server 测试提供身份验证 token 的 GraphQL 查询?

javascript - 在 javascript 中更改 RGB 颜色的色调

javascript - 输入类型文本数据未插入 SQL DB 中

reactjs - 类型错误 : Cannot read property 'forEach' of undefined jest

javascript - 如何在 Jest 中模拟导出的常量

jestjs - Jest 24.0.0 Plugin/Preset 文件不允许导出对象,只能导出函数

javascript - "If"语句类似于 "While"循环