Let's imagine I have a module as below:
// utils.ts
function innerFunction() {
return 28;
}
function testing() {
return innerFunction();
}
export {testing}
I would like to write a unit test to test testing
and just mock return value of innerFunction
, expecting that any call to innerFunction
will just resolve to a certain value, something like below:
jest.mock('../utils', () => {
const originalModule = jest.requireActual('../utils');
return {
// __esModule: true,
...originalModule,
innerFunction: jest.fn().mockReturnValue(33),
};
});
import { testing } from '../utils';
it('should be okay', () => {
expect(testing()).toBe(33);
});
I was expecting jest.requireActual
will be able to read all functions and innerFunction: jest.fn().mockReturnValue(33)
will actually cause any innerFunction
invocation to just return 33
as value, but from the little experiment as above it seems like it's not the case.
In actual call innerFunction
will be returning 28
, but in Jest environment I would like innerFunction
to be able to resolve to any value that I would like to