testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
18.94k stars 1.1k forks source link

userEvent.type and fireEvent.change both cause subsequent uses to not work #1227

Closed jpage-godaddy closed 1 year ago

jpage-godaddy commented 1 year ago

Relevant code or config:

Both I and a co-worker are experiencing the same issue, where using userEvent.type works the first time around, but in subsequent uses it fails to trigger the onChange event of inputs. For example, if we have two unit tests, the first will pass and the second will not, but when run in isolation the two tests work just fine.

await userEvent.type(screen.getByLabelText('Bug Report'), 'mock bug report');

What you did:

Use userEvent.type on an input.

What happened:

The onChange event for the input fails to be triggered, but only if a userEvent.type had previously occurred. Here's the details we've gathered:

Reproduction:

I'll work on this... creating reproductions is a very laborious process, especially in private code bases... hoping it'll be faster to get this out sooner rather than later in case there are specific things you want us to check since we've already tried to dig into this quite a bit.

Problem description:

This makes testing using RTL impractical.

Suggested solution:

Figure out what is causing text input state from one test breaking subsequent tests.

jpage-godaddy commented 1 year ago

Found the root cause. This code base also includes some enzyme-based tests in the mixture. Apparently the setup for enzyme wreaks havoc on the DOM. Removing its setup steps allows the tests to pass just fine.