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.

Error thrown when using react hooks

See original GitHub issue

Description

Given a react component using hooks When a you attempt to use a user event e.g. type Then you receive the error below

Warning: An update to %s inside a test was not wrapped in act(...).

Reason

As far as I understand this is because we are using the fireEvent from @testing-library/dom directly and not from @testing-library/react which means that it is not being in act.

Solution

A possible solution would be to allow for the user to provide a fireEvent implementation or allow for this to be configured in test setup

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:40 (20 by maintainers)

github_iconTop GitHub Comments

5reactions
AmirTugicommented, Oct 30, 2020

This issue is still happening. Why is this lib still using the fireEvent from the @testing-library/dom? Any idea if that’s going to be changed/fixed?

5reactions
matburnhamcommented, Jun 18, 2020

If I strip it right back, all is fine:

BasicForm.js:

import React from 'react';

const BasicForm = () => {
  const onSubmit = e => {
    e.preventDefault();
  };
  return (
    <form onSubmit={onSubmit}>
      <input type='submit' />
    </form>
  );
};

export default BasicForm;

BasicForm.test.js:

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import BasicForm from './BasicForm';

it("doesn't fall over on userEvent.click", async () => {
  render(<BasicForm />);
  userEvent.click(screen.getByRole('button'));
});

The problem is only present when I use handleSubmit from react-hook-form:

BasicForm.js:

import React from 'react';
import { useForm } from 'react-hook-form';

const BasicForm = () => {
  const { handleSubmit } = useForm();
  const onSubmit = data => {};
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input type='submit' />
    </form>
  );
};

export default BasicForm;

npm run test BasicForm:

 PASS  src/BasicForm.test.js
  ✓ doesn't fall over on userEvent.click (170ms)

  console.error node_modules/react-dom/cjs/react-dom.development.js:88
    Warning: An update to BasicForm 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 BasicForm (at BasicForm.test.js:7)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Handling in React Hooks - Medium
Error Handling in Hooks is quite Simple. In this article, we are going to demonstrate how to handle errors in React Hooks.
Read more >
Handle Error While Testing Your React Hooks
Handle errors when a hook is called​​ The reason is that it can throw an exception that cannot be handled. It is impossible...
Read more >
Why can React hooks be used conditionally with throw errors?
By throwing an error you are bailing out of the rest of the render, so whether or not a React hook is callable...
Read more >
Throwing Error from hook not caught in error boundary #14981
This may be more of a question than an issue but is it possible to throw errors in a hook and have them...
Read more >
Invalid Hook Call Warning - React
You are probably here because you got the following error message: Hooks can only be called inside the body of a function component....
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