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.

MockedProvider causing test to complain about not using `act()`.

See original GitHub issue

Intended outcome:

Actual outcome:

When running test using MockProvider I expect test to not error out with the following error:

 Warning: An update to ResearchCategoryList inside a test was not wrapped in act(...).
 When testing, code that causes React state updates should be wrapped into act(...):

How to reproduce the issue:

The following test will cause test to complain:

it('renders without error', async () => {
  let component
  act(() => {
    component = create(
      <MockedProvider mocks={mocks} addTypename={false}>
        <ResearchCategoryList category="Education" />
      </MockedProvider>
    )
  })
  await wait(0)
  const tree = component.toJSON()
  expect(tree).toMatchSnapshot()

Versions

"@apollo/react-testing": "^3.1.3",

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:30
  • Comments:26 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
CosmaTrixcommented, Feb 12, 2020

@donedgardo I’ve beeing having the same issue, using Jest and @testing-library/react. Right now I’m suppressing those warnings, as described in the in the @testing-library/react documentation.

In my jest global setup file, I’ve added

const originalError = console.error;

beforeAll(() => {
  console.error = (...args: any[]) => {
    if (/Warning.*not wrapped in act/.test(args[0])) {
      return;
    }

    originalError.call(console, ...args);
  };
});

afterAll(() => {
  console.error = originalError;
});

I’m not completely sure about this approach (this might suppress warnings that need to be taken care of), but that does the trick! 😄

21reactions
CosmaTrixcommented, Feb 16, 2020

@donedgardo Thanks for pointing that issue out. I’ve been using the wait exported from @testing-library/react and that fixes the issue.

import { render, wait } from "@testing-library/react";

it ("renders without errors", () => {
  render(
    <MockedProvider mocks={mocks} addTypename={false}>
      <ResearchCategoryList category="Education" />
    </MockedProvider>
  );

  await wait()

  // run expectations
});

In your case, I think you can try and wrap the await wait() in act as suggested in this comment.

await act(wait);

Both solutions work for me! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing React components - Apollo GraphQL Docs
The MockedProvider component enables you to define mock responses for individual queries that are executed in your test. This means your test doesn't...
Read more >
React Testing Library and the “not wrapped in act” Errors
Case 1: Asynchronous Updates. Test code somehow triggered a testing component to update in an asynchronous way. Here is an example: const MyComponent...
Read more >
Debugging Apollo GraphQL MockedProvider - Swarmia
We're big on Apollo GraphQL here at Swarmia, but we've sometimes struggled with mocking. This blog post is here to help!
Read more >
Apollo MockedProvider testing issues (my rendered ...
First, the empty array/loading issue was not an issue; I traced everything back to testing-library resetting/re-rendering the component ...
Read more >
Unit Testing - React-admin
React-admin relies heavily on unit tests (powered by Jest and ... This will cause seemingly random test failures when you use useStore() in...
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