Function I want to test is createCarAndDrive()
which uses Car
module. I'd like to mock one function in the Car
module so its actual function is not called but the mock function.
So, the Car
function returns two functions gas()
and brake()
. Car
is implemented by using factory function pattern. So those two functions are wrapped in Car
and won't be exposed until Car
is called.
Is it somehow possible to mock the function brake()
to return false
?
Here's the implementation.
// Car.js
function Car({ name, model }) {
function gas(speed) {
return `${name} ${model} goes forward at a speed of ${speed}km/h`;
}
function brake() {
return true;
}
return {
gas,
brake,
};
}
// driver.js
function createCarAndDrive() {
const car = Car({ name: 'Fiat', model: 'Punto' });
car.gas(123);
return car.brake();
}
// driver.test.js
describe('drive', () => {
beforeEach(() => {
// How to mock function inside a function?
jest.mock('./Car', () => ({
brake: jest.fn().mockImplementation(() => false),
}));
});
test('it should not brake', () => {
const itHitBreak = createCarAndDrive();
expect(itHitBreak).toBe(false);
});
});