用玩笑嘲笑导出的函数

时间:2018-12-12 11:52:09

标签: javascript jestjs

我有以下代码:

// index.js 

function a() {
  return "a";
}

function b() {
  console.log(a());
}

module.exports = {
  a,
  b
};

// index.spec.js

import ab from "./index.js";

describe("jest.spyOn", () => {
  it("ab", () => {
    ab.a = jest.fn(() => "f");

    ab.b();
  });
});

我正在尝试为b编写单元测试,我想对a进行模拟/存根,以根据我正在测试的内容返回某些值。上面的代码不起作用,因为它仍然返回"a"。谁能解释这是为什么以及我如何模拟此函数调用?

codesandbox链接:https://codesandbox.io/s/n7wjp51mzp

0 个答案:

没有答案
相关问题