I am doing testing using JEST in npm. Below is scenarios
I have a TestScript.js file
function someComplexFunction() {
console.log("some important message");
}
module.exports = someComplexFunction
to test this function in there in TestScript.test.js file
const someComplexFunction = require("./TestScript")
test('Console log should have been called', () => {
const logSpy = jest.spyOn(console, 'log');
someComplexFunction();
expect(logSpy).toHaveBeenCalledWith('some important message');
logSpy.mockRestore();
});
when I am runnig the test I getting failure
npm test
Console log should have been called
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "some important message"
Number of calls: 0
32 | someComplexFunction();
33 |
> 34 | expect(logSpy).toHaveBeenCalledWith('some important message');
| ^
35 |
36 | logSpy.mockRestore();
37 | });
at Object.toHaveBeenCalledWith (src/test/TestScript.test.js:34:17)
Please let me know what I am making mistake, why mocking is not working.
I tried changing the spyOn function but still not working, also tried with warn but still no luck.
Finally it worked in this way: