This question already has an answer here:
I have the following module called index.js
export function iWantToMockThis(a) {
return a + 1;
}
export function foo(a) {
return iWantToMockThis(a);
}
and the following test
import {foo} from '../src/index';
describe("testing mock es6", () => {
jest.doMock('../src/index', () => ({
foo: jest.requireActual('../src/index').foo,
iWantToMockThis: jest.fn(() => 99)
}));
test('mocking barProp', ()=> {
expect(foo(2)).toBe(99); // <-- it should be 99 but its 3
})
});
This is just a small example of what I'm trying to achieve a bigger scale, by design iWantToMockThis
has to be in the same module, but I do not find the way to mock it and return what I want to the foo
method.
Any idea?
Some context:
- I tried many things, eg
jest.mock
before the es6 import. - Default jest configuration
complete example: https://github.com/juanpicado/jest_mock_es6_example