On mocking function toHaveBeenCalledTimes() returns 0
See original GitHub issueMy Component
import React from "react";
import { withRouter } from "react-router-dom";
import { Icon } from "antd";
const handleGoBack = (history, returnPath) => {
if (returnPath) {
history.push(returnPath);
} else {
history.goBack();
}
};
const GoBack = ({ history, returnPath }) => (
<span onClick={() => handleGoBack(history, returnPath)} style={{ cursor: "pointer" }}>
<Icon type="arrow-left" />
</span>
);
export default withRouter(GoBack);
Test
import React from "react";
import { shallow, mount } from "enzyme";
import GoBack from "./GoBack";
import { BrowserRouter as Router } from "react-router-dom";
const returnPath = "/manage-rfq";
describe("GoBack", () => {
let wrapper;
const history = {
goBack: jest.fn(),
push: jest.fn()
};
it("should not push in history", () => {
wrapper = mount(
<Router>
<GoBack />
</Router>
);
wrapper.simulate("click");
// this returns 0 although that method is getting called`
expect(history.goBack).toHaveBeenCalledTimes(1);
});
});
Error:
● GoBack › should not push in history
expect(jest.fn()).toHaveBeenCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
29 | wrapper.simulate("click");
> 31 | expect(history.goBack).toHaveBeenCalledTimes(1);
| ^
32 | });
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Jest Mock function is called but jest toHaveBeenCalledTimes ...
Jest Mock function is called but jest toHaveBeenCalledTimes returns 0 ... function when user clicks on Logout", async () => { const ...
Read more >Jest Mock Function Is Called But Jest ... - ADocLib
Jest Mock Function Is Called But Jest Tohavebeencalledtimes Returns 0 ... A situation that the toHaveBeenCalledTimes actual value not correct of state.
Read more >Mock Functions - Jest
You can create a mock function with jest.fn() . If no implementation is given, the mock function will return undefined when invoked.
Read more >Jest — mockReset for afterEach affect toHaveBeenCalledTimes
Solution. I added the function.mockReset() in the afterEach hook and the count was matched after. This way will reset toHaveBeenCalledTimes for the mock...
Read more >jest.Mock.mock JavaScript and Node.js code examples
await waitFor(() => crashListener.mock.calls.length > 0) ... it('should do nothing if not a function', () => { const session = cls.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’d suggest avoiding
simulate
; it does not actually simulate anything. (also, if you stickjsx
after your triple backticks, it syntax highlights and makes code snippets readable)Can you try
.invoke('onClick')()
instead?@RehanRabbani use
wrappingComponent
forRouter
, and avoidsimulate
since it doesn’t simulate anything - if you want to invokeonClick
, use.invoke('onClick')()
.