I am not expert in jest, but my recommendation to think about:
1) When the function is exported as default I use something like:
import Funct1 from "../../../src/components/Funct1";
...
jest.mock("../../../src/components/Funct1");
...
expect(Funct1).toHaveBeenCalledWith(params);
2) When the module (utils.js) has multiple exports as
export const f1 = () => {};
...
export const f8 = () => {};
You can try
import * as Utils from "../../../src/components/utils"
const f8Spy = jest.spyOn(Utils, 'f8');
...
expect(f8Spy).toHaveBeenCalledWith(params);
Similar discussion here
Answer from Dima Dorogonov on Stack OverflowI am not expert in jest, but my recommendation to think about:
1) When the function is exported as default I use something like:
import Funct1 from "../../../src/components/Funct1";
...
jest.mock("../../../src/components/Funct1");
...
expect(Funct1).toHaveBeenCalledWith(params);
2) When the module (utils.js) has multiple exports as
export const f1 = () => {};
...
export const f8 = () => {};
You can try
import * as Utils from "../../../src/components/utils"
const f8Spy = jest.spyOn(Utils, 'f8');
...
expect(f8Spy).toHaveBeenCalledWith(params);
Similar discussion here
Wrap your function with jest.fn. Like this:
const simpleFn = (arg) => arg;
const simpleFnSpy = jest.fn(simpleFn);
simpleFnSpy(1);
expect(simpleFnSpy).toBeCalledWith(1); // Passes test
Cannot spy on individual functions that are individually exported
How to mock only a non-default function in Jest
Alternative to spyOn().and.callfake in Jestjs
When Unit Testing with Jest, How do you Mock Getter Properties?
Why aren't you just mocking the entire authentication service?
More on reddit.comWhat Does Jest.clearallmocks() Function Do?
What Does the Jest.runonlypendingtimers() Function Do in the Program?
What Does Jest.restoreallmocks() Do?
I am trying to mock a non default function in jest which is part of a larger module and I don't want to mock any of the rest of it. I tried something along the lines of the solution found here (https://stackoverflow.com/questions/59312671/mock-only-one-function-from-module-but-leave-rest-with-original-functionality). The problem is that importing the default function gives you the whole module instead of the default function.
For example:
// myModule.js
export const foo = () => {
console.log("in foo");
};
const bar = () => {
console.log("in bar");
};
export default bar;// useMyModule.test.js
import bar, { foo } from "./myModule";
import "./useMyModule";
jest.mock("./myModule", () => ({
...jest.requireActual("./myModule"),
foo: jest.fn(() => {
console.log("in test foo");
}),
}));
test("see if this works", () => {
console.log(bar);
foo();
});
// useMyModule.js import bar from "./myModule"; console.log(bar); //bar(); // <== commented out because bar is not the function I expect it to be
This is what the output looks like:
PASS src/useMyModule.test.js
● Console
console.log
{
default: [Function: bar],
foo: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function (anonymous)],
mock: [Getter/Setter],
mockClear: [Function (anonymous)],
mockReset: [Function (anonymous)],
mockRestore: [Function (anonymous)],
mockReturnValueOnce: [Function (anonymous)],
mockResolvedValueOnce: [Function (anonymous)],
mockRejectedValueOnce: [Function (anonymous)],
mockReturnValue: [Function (anonymous)],
mockResolvedValue: [Function (anonymous)],
mockRejectedValue: [Function (anonymous)],
mockImplementationOnce: [Function (anonymous)],
mockImplementation: [Function (anonymous)],
mockReturnThis: [Function (anonymous)],
mockName: [Function (anonymous)],
getMockName: [Function (anonymous)]
}
}
at Object.<anonymous> (src/useMyModule.js:3:9)
console.log
{
default: [Function: bar],
foo: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function (anonymous)],
mock: [Getter/Setter],
mockClear: [Function (anonymous)],
mockReset: [Function (anonymous)],
mockRestore: [Function (anonymous)],
mockReturnValueOnce: [Function (anonymous)],
mockResolvedValueOnce: [Function (anonymous)],
mockRejectedValueOnce: [Function (anonymous)],
mockReturnValue: [Function (anonymous)],
mockResolvedValue: [Function (anonymous)],
mockRejectedValue: [Function (anonymous)],
mockImplementationOnce: [Function (anonymous)],
mockImplementation: [Function (anonymous)],
mockReturnThis: [Function (anonymous)],
mockName: [Function (anonymous)],
getMockName: [Function (anonymous)]
}
}
at Object.<anonymous> (src/useMyModule.test.js:12:13)However, if you uncomment the bar() invocation above, you get this:
FAIL src/useMyModule.test.js
● Test suite failed to run
TypeError: (0 , _myModule.default) is not a function
2 |
3 | console.log(bar);
> 4 | bar();
| ^
5 |
at Object.<anonymous> (src/useMyModule.js:4:4)
at Object.<anonymous> (src/useMyModule.test.js:2:1)So, how should I mock the foo function, but leave the default bar function intact?