function a() {
return "a";
}
function b() {
return a();
}
export { a, b };
Let's say I have a utils.js like this, and I just want to mock a()
function that is being used by b()
function within the same file.
After googling all around, I found an article that seemed to be just for this case.
import {a, b} from "./utils"
jest.mock("./utils", () => {
const original = jest.requireActual("./utils");
return {
...original,
a: jest.fn().mockReturnValue("modified a")
};
});
describe("utils", () => {
it("sholuld use mocked a()", () => {
expect(b()).toEqual("modified a")
expect(a).toHaveBeenCalledTimes(1)
});
});
I did it like this, but it seems b()
function is still calling the real a()
function despite the fact that I am mocking a()
function specifically.
Expected: "modified a"
Received: "a"
11 | describe("utils", () => {
12 | it("sholuld use mocked a()", () => {
> 13 | expect(b()).toEqual("modified a")
| ^
14 | expect(a).toHaveBeenCalledTimes(1)
15 | })
16 | })
Is it even possible to mock a function for such this case?