I am using Jest for my testing environment and I am trying to see if a method in the tested module calls another method at least once.
Here is the nested.js
module:
export function getUserData() {
console.log("CALLING API");
const user = {
name: "user",
};
setUserData(user);
}
export function setUserData(user) {
console.log("Setting user data");
}
And here is the nested.test.js
:
import * as nested from "../nested";
let storeUserDataSpy = jest.spyOn(nested, "setUserData");
describe("Get user data calls child method", () => {
it("should call the child method", async () => {
nested.getUserData();
expect(storeUserDataSpy).toHaveBeenCalledTimes(1);
});
});
The error that I am getting is that the method has not been called at all:
Expected number of calls: 1
Received number of calls: 0
I tried various ways of resolving the issue including jest.mock
and including the actual methods from the nested.js
module.
What I don't want to do is to spyOn
console logs as these won't be included in the production code.
I uploaded the code with all the Jest and Babel config to a GitHub repo.
Here are the steps to try it locally:
git clone git@github.com:verebes1/tests-jest.git
cd tests-jest
npm install
npm run test