Addition.js
module.exports = function add(a, b){
return a + b;
};
CustomThing.js
var addition = require("./addition");
module.exports = class CustomThing {
performAddition(a, b){
return addition(a, b);
}
}
CustomThingTest.js
test('1 + 2 = 3', () => {
//Arrange
var CustomThing = require('./CustomThing');
var customThing = new CustomThing();
//Act
var result = customThing.performAddition(1, 2);
//Assert
expect(result).toBe(3);
});
test('addition function mocked', () => {
//Arrange
jest.mock('./addition', () => {
return jest.fn(() => 42);
});
var CustomThing = require('./CustomThing');
var customThing = new CustomThing();
//Act
var result = customThing.performAddition(1, 2);
//Assert
expect(result).toBe(42);
});
How do I handle mocking the 'addition' dependency when writing tests? The error message received is as follows.
addition function mocked
expect(received).toBe(expected)
Expected value to be (using ===):
42
Received:
3
An interesting observation is that running each test individually with .only works flawlessly independently.
I have relied on proxyquire in the past for such scenarios but would like to explore alternative solutions if possible.