无法开玩笑地模拟模块

Unable to mock module in jest

我无法开玩笑地模拟模块
当我使用下面的代码测试用例失败时

jest.mock("./module", () => ({
 method1: jest.fn().mockImplementation(() => "method1"),
}));

但是当我使用箭头函数时,四边形大小写通过了。

jest.mock("./module", () => ({
  method1: () => "method1",
}));

这两种方法有什么区别还是我做错了什么。

下面是实现。

//utils.js
export const method1 = () => "method 1";

//App.js
import { method1 } from "./utils.js";
export const showMsg = () => method1();




//App.test.js ( Test case fails)
import { showMsg } from "./App";

jest.mock("./utils.js", () => ({
  method1: jest.fn().mockImplementation(() => "method1"),
}));

describe("test mock", () => {
  it("returns the correct value for Method 1", () => {
    expect(showMsg()).toBe("method1");
  });
});


//App.test.js (Test case success)
import { showMsg } from "./App";

jest.mock("./utils.js", () => ({
  method1: () => "method1",
}));

describe("test mock", () => {
  it("returns the correct value for Method 1", () => {
    expect(showMsg()).toBe("method1");
  });
});

提前致谢

我能够弄清楚这个问题。实施没有任何问题。 package.json 文件中只缺少一个配置。

"jest": {
    "resetMocks": false,
    "restoreMocks": false
  }