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.

An update to Dropzone inside a test was not wrapped in act(...).

See original GitHub issue

What is the current behavior?

When trying to test a component containing Dropzone, there’s a console.errror (but tests pass):

console.error node_modules/react-dom/cjs/react-dom.development.js:506
  Warning: An update to Dropzone inside a test was not wrapped in act(...).
  
  When testing, code that causes React state updates should be wrapped into act(...):
  
  act(() => {
    /* fire events that update state */
  });
  /* assert on the output */
  
  This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
      in Dropzone (at InputFile.js:86)
      in div (at InputFile.js:85)
      in InputFile (at InputFile.test.js:68)

Test example which triggers the error:

import { render, act } from '@testing-library/react';

describe('InputFile', () => {
  it('example', async () => {
    act(() => {
      const rendered = render(<InputFile {...props} />);
    });
  });
});

InputFile is:

class InputFile extends React.Component {
  onDrop = () => { /*…*/ }

  render() {
    return (
      <Dropzone
        onDrop={this.onDrop}
      >
        {({ getRootProps, getInputProps }) => {
          if (uploading) {
            return <div>...uploading</div>;
          }

          return (
            <div {...getRootProps()}>
              <input {...getInputProps()} />
            </div>
          );
        }}
      </Dropzone>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

4reactions
MaximeConancommented, Jul 15, 2021

@alex-polunochev it works without await the Promise. So you don’t need to have a setTimeout.

  await act(async () => {
    fireEvent.change(input, {
        target: { files: [file] },
    })
  })
3reactions
alex-polunochev-zzcommented, Jan 21, 2021

I was testing the file upload event and got the same error, the plain component test rendering was fine.
Fixed the problem with input change by creating a Promise with short timeout:

    await act(async () => {
      // upload the file by updating the value attribute of the input
      fireEvent.change(screen.getByPlaceholderText('Browse'), {
        target: { files: [file] }
      });
      await new Promise((r) => setTimeout(r, 10));
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix the "not wrapped in act(...)" warning - Kent C. Dodds
There are a few reasons you're getting this warning. Here's how you fix it.
Read more >
Jest + react-testing-library: Warning update was not wrapped ...
I was getting this warning: Warning: An update to Formik inside a test was not wrapped in act(...)
Read more >
React Testing Library and the “not wrapped in act” Errors
I recently upgraded React and React testing library. Very happy about the upgrade. But I start to see test errors like this: In...
Read more >
Fix Warning in React: Update was not wrapped in act()
To solve this problem we can use waitFor from @testing-library/react . This will wait for whatever is inside it to be true or...
Read more >
Fix the "not wrapped in act(...)" warning when testing Custom ...
Any time your test interacts with a React component in a way that results in a state update, you need to make sure...
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