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.

Can't assert that methods are being called

See original GitHub issue

Describe the bug

I’m trying to check if two methods are being called from componentDidMount. The first method gets called and it’s successful, but the second one fails. If I try testing just the second method it fails as well. I have to mention that I’m not using arrow functions and I’m binding in the constructor.

The following is an example of the test I wrote

it("calls handleAlertsRefetch and handleRefetch on mount", () => {
      const handleAlertsRefetch = jest.spyOn(
        Component.prototype,
        "handleAlertsRefetch"
      );
      const handleRefetch = jest.spyOn(
         Component.prototype,
         "handleRefetch"
      );
      const wrapper = createContext(BottomOverview, props);

      expect(handleAlertsRefetch).toHaveBeenCalled();
      expect(handleRefetch).toHaveBeenCalled();
    });

The createContext is a method that puts the component in an Apollo and react-router context:

export const createContext = (Component, props) => {
  Component.contextTypes = {
    router: PropTypes.object
  };
  const context = createRouterContext();
  const childContextTypes = {
    router: PropTypes.object
  };
  const wrapper = mount(
    <MockedProvider>
      <Component {...props} />
    </MockedProvider>,
    {
      context,
      childContextTypes
    }
  );
  return wrapper;
};

Expected behavior Both methods should be called and tests should be pass.

Additional context I use React 16 with the CRA boilerplate.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
yoloOnTheBattlefieldcommented, Aug 14, 2018

hey buddy I fixed the component code and now it works, thanks a lot for your help. Where can I find more information about this aspect of asynchronicity?

1reaction
yoloOnTheBattlefieldcommented, Aug 2, 2018

Hey, I removed everything from the body of those methods, still the same issue with it. Thank you for your response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to assert that a method has not been called after an ...
So far in my unit test, I've mocked the API and wrote when(APIService.postDetails(any())).thenThrow(Exception.class) and to assert I've verified ...
Read more >
Verify that functions were called | Mocking | MockK Guidebook
Using verify to verify that a function was called looks a lot like using every for stubbing. A simple example is to call...
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
After performing an action, you can make assertions about which methods / attributes were used and arguments they were called with. You can...
Read more >
Testing Guide
In Java, assert statements can be enabled via the JVM parameters -ea (or -enableassertions ) and -da (or -disableassertions ). Assertion ...
Read more >
Debug.Assert Method (System.Diagnostics) | Microsoft Learn
Assert (Boolean). Checks for a condition; if the condition is false , displays a message box that shows the call stack.
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