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.

jest mock client execute mutation fails assertion

See original GitHub issue

@JoviDeCroock @kitten expect(mockClient.executeMutation).toBeCalledTimes(1); /// gives 0

may be I am doing something wrong. but can you guys guide me

sample code

const apiClient: any = {
        executeMutation: jest.fn(() =>
          fromValue({
            fetching: true,
            data: {
              login: {
                token: '',
              },
            },
          }),
        ),
      };
      const { getByTestId } = render(
        <Provider value={apiClient}>
          <Login />
        </Provider>,
      );
      const submitEl = getByTestId('submit');
      await act(async () => {
        const userNameEl = getByTestId('email');
        const passwordEl = getByTestId('password');
        fireEvent.change(userNameEl, {
          target: {
            value: 'username',
          },
        });
        fireEvent.change(passwordEl, {
          target: {
            value: 'password',
          },
        });
        await fireEvent.click(submitEl);
      });
      expect(mockClient.executeMutation).toBeCalledTimes(1);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
baldurpancommented, Jan 19, 2021

There is nothing about that second argument in the testing docs, so I can see why that would cause confusion. It currently demonstrates:

expect(mockClient.executeMutation).toBeCalledWith(expect.objectContaining({ variables }));

Perhaps the docs should be updated?

1reaction
kittencommented, Jun 16, 2020

@chiragprodsmiths looks like you’re just missing the second argument for executeMutation here: https://github.com/FormidableLabs/urql/blob/062c1359a7ccd41044d2eb963ab0da5629143cc5/packages/core/src/client.ts#L343-L344 (Note the red , {} at the end of Jest’s Received output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest mock client execute mutation fails assertion #868 - GitHub
Hey,. How does the component look? I assume it works when you're using this in the browser/your relevant environment? Is it possible there's ......
Read more >
Testing React components - Apollo GraphQL Docs
This article describes best practices for testing React components that use Apollo Client. The examples below use Jest and React Testing Library, ...
Read more >
Testing Apollo: How to test if a mutation was called with ...
If you assign a mock function here you can simply assert whether or not that function was called. But enough talking, let's have...
Read more >
Mock Apollo Client in tests (#68) · Issues - GitLab
For checking query result, we assert it as a data property and for mutations we're able to mock resolved value. You can find...
Read more >
Testing Apollo Mutation graphql errors - Stack Overflow
When mutation fails, react-apollo throws an error if no onError prop is provided to Mutation component (source code here).
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