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.

act() warnings when using React Hook Form

See original GitHub issue

🐛 Bug Report

When a form that’s using form.register() from React Hook Form is conditionally rendered within a test, act() warnings are printed in the console.

A workaround seems to be to:

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

const { page } = getPage({route: '/'})
render(page)

In the reproduction repo both variants (render from npt, render from tl) are used.

To Reproduce

  1. Create a page with a form. Set a ref on an input in the form using form.register().
  2. Mount and unmount the form within the page with a toggle switch In a test:
  3. Render the page with render() from getPage()
  4. act on the toggle switch to mount the form
  5. console logs an act() warning

Expected behavior

No warnings

Link to repl or repo

https://github.com/osadi/npt-react-hook-form

Should work with npm i && npm run test

Run npx envinfo --preset jest

npx: installed 1 in 0.964s

  System:
    OS: Linux 5.4 Manjaro Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  Binaries:
    Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v14.15.0/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
Meemawcommented, Mar 3, 2021

It might be as simple as wrapping our hydrate call  into react’s act test utility, but I would like to do it on a reproducer without external dependency so we have a stable test for it.

0reactions
Meemawcommented, Mar 6, 2021

Ah, I though you are receiving act warning after just after rendering  – not after performing client interactions. Those are out of scope of the library and its a known issue with react-hook-form, e.g. see https://github.com/testing-library/user-event/issues/457 for example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

act warnings with testing-library · Discussion #3108 - GitHub
Hi, I read the following doc about testing using react-hook-form with testing-library: ... it('renders without warnings', () => { render( <MyTestForm ...
Read more >
Advanced Usage - React Hook Form
React Hook Form's FormProvider is built upon React's Context API. It solves the problem where data is passed through the component tree without...
Read more >
How to use soft validation (aka tips or warning messages) in ...
I would like to find a solution that plays well with React Hook Form V7. The Problem: React Hook Form V7 only supports...
Read more >
Fix the "not wrapped in act(...)" warning when testing Custom ...
In this lesson you'll learn how you can use act with @testing-library/react-hooks to avoid potential bugs in your custom React hooks. This is...
Read more >
Using React Hook Form in component tests | by Adam J. Arling
This article illustrates our team's approach to organizing and testing nested form components, using React Hook Form's <FormProvider /> and useFormContext() ...
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