question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On mocking function toHaveBeenCalledTimes() returns 0

See original GitHub issue

My 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Feb 11, 2020

I’d suggest avoiding simulate; it does not actually simulate anything. (also, if you stick jsx after your triple backticks, it syntax highlights and makes code snippets readable)

Can you try .invoke('onClick')() instead?

0reactions
ljharbcommented, Sep 30, 2020

@RehanRabbani use wrappingComponent for Router, and avoid simulate since it doesn’t simulate anything - if you want to invoke onClick, use .invoke('onClick')().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found