My goal is to mock foo, but not mock bar. This is a simplified example that demonstrates my issue, but I also have several other functions that i do NOT want to mock. I call the function bar from inside foo as shown below:
utils.js
const foo = async () => {
const result = await bar();
return result;
};
const bar = async () => {
return 'THINGIMTRYINGTOMOCK';
};
module.exports = {
foo,
bar,
};
This is my file for utils.test.js
const { foo, bar } = require('../../utils');
jest
.spyOn(require('../../utils'), 'bar')
.mockResolvedValue('1234567890ABCDEF');
describe('who designed this shit', () => {
test('test description', async () => {
const result = await foo();
expect(result).toBe('1234567890ABCDEF');
});
});
However, this test fails, and the result is "THINGIMTRYINGTOMOCK", which means it's not being mocked correctly.
I also tried this alternative approach that I've seen posted on stack overflow for my test.js file, but I get the same result:
const { foo, bar } = require('../../utils');
jest.mock('../../utils', () => ({
...jest.requireActual('../../utils'),
bar: jest.fn().mockResolvedValue('1234567890ABCDEF'),
}));
describe('who designed this shit', () => {
test('test description', async () => {
const result = await foo();
expect(result).toBe('1234567890ABCDEF');
});
});