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.
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:
This happens on plain HTML input elements.
The element references are legitimate; document.querySelector yields the same input, so the issue isn't that the wrong input is being acted upon.
Creating a brand new jsdom from scratch for each test does not alleviate the issue.
Using fireEvent from RTL has the exact same issue.
Using Simulate.change from react-dom/test-utils does not have the same issue; it works just fine, even with the same referenced DOM element.
We've tried calling cleanup manually to no effect.
There is no lingering DOM between tests; we've even tried manually using document.innerHTML = ''.
The issue isn't just that the event handler doesn't fire; the DOM itself doesn't change; the value of the input remains blank, and outputting outerHTML reveals it being blank as well.
Wrapping the interactions in act doesn't change anything.
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.
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.
@testing-library/react
version:12.1.5
mocha@10.2.0
jsdom@22.1.0
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 theonChange
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.What you did:
Use
userEvent.type
on an input.What happened:
The
onChange
event for the input fails to be triggered, but only if auserEvent.type
had previously occurred. Here's the details we've gathered:document.querySelector
yields the same input, so the issue isn't that the wrong input is being acted upon.jsdom
from scratch for each test does not alleviate the issue.fireEvent
from RTL has the exact same issue.Simulate.change
fromreact-dom/test-utils
does not have the same issue; it works just fine, even with the same referenced DOM element.cleanup
manually to no effect.document.innerHTML = ''
.value
of the input remains blank, and outputtingouterHTML
reveals it being blank as well.act
doesn't change anything.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.