-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.clear in v14 produces an act warning when used with React hook form. #938
Comments
Thanks for the report. ❤️ This is an issue with the state update happening during the first import { createEvent, getConfig, render, screen } from "@testing-library/react";
import { eventMap } from '@testing-library/dom/dist/event-map.js'
import Form from "./Form";
const config = getConfig()
const originalAsyncWrapper = config.asyncWrapper
config.asyncWrapper = cb => {
console.log('start asyncWrapper', cb)
const p = originalAsyncWrapper(cb)
console.log('end asyncWrapper', cb)
return p
}
it("should show an error message when title field is cleared", async () => {
render(<Form />);
expect(screen.queryByText("Title is required")).not.toBeInTheDocument();
const input = screen.getByLabelText<HTMLInputElement>("Title")
expect(input).toHaveValue('A title')
await clearApi(input)
console.log('after API')
expect(await screen.findByText("Title is required")).toBeInTheDocument()
});
async function clearApi(el: HTMLInputElement) {
const p = config.asyncWrapper(() => clearImpl(el))
console.log('after asyncWrapper')
// await here let's the state update happen
await p
}
async function clearImpl(input: HTMLInputElement) {
// This happens too but is irrelevant for the issue:
// input.focus()
// input.setSelectionRange(0, input.value.length)
// config.eventWrapper(() => {
// input.dispatchEvent(createEvent('beforeinput', input, {
// data: '',
// inputType: 'deleteContentBackward',
// }, eventMap['input']))
// })
Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(input),
'value',
)?.set?.call(input, '')
config.eventWrapper(() => {
input.dispatchEvent(createEvent('input', input, {
data: '',
inputType: 'deleteContentBackward',
}, eventMap['input']))
})
// This line makes the state update happen durinc asyncWrapper
// await Promise.resolve()
} Maybe we should add a |
While waiting for a fix, you can monkey-patch calls that produce act warnings inside, well, act calls 😄 : import userEvent from '@testing-library/real-user-event';
import { act } from '@testing-library/react';
const original = {
click: userEvent.click,
clear: userEvent.clear,
type: userEvent.type,
};
userEvent.click = async (...args) =>
await act(async () => await original.click(...args));
userEvent.clear = async (...args) =>
await act(async () => await original.clear(...args));
userEvent.type = async (...args) =>
await act(async () => await original.type(...args));
export default userEvent; And map your module inside your jest config: moduleNameMapper: {
'^@testing-library/user-event$': '<rootDir>/tests/act-user-event.js',
'^@testing-library/real-user-event$': require
.resolve('@testing-library/user-event'),
}, |
🎉 This issue has been resolved in version 14.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@all-contributors add @mikaelrss bug |
I've put up a pull request to add @mikaelrss! 🎉 |
Thank you for this! 🎉 This solves all the issues I experienced with act-warnings when doing |
Might be a bug in the component. Might be a bug in the environment. Might be a bug here.
|
I also use unstable React Hook Form Here is screenshot of the exception: #970 |
More info about this bug is that the input field is frozen for first 2 characters (interactions). Just typing a bit in the field fixes it. Either userEvent v14 or React Hook Form v8 are broken. // NOTE: this fixes a bug in userEvent.clear() or React Hook Form
// field is frozen for first 2 characters
// user0 name + 123 -> user0 name3
await userEvent.type(nameInput, '123');
// edit name
await userEvent.clear(nameInput);
expect(nameInput).toHaveValue(''); // now it works |
For me, this error reappears when I update from while using
Any idea what may cause this? |
Reproduction example
https://github.com/mikaelrss/rhf-rtl-act-bug-repro
Prerequisites
git clone git@github.com:mikaelrss/rhf-rtl-act-bug-repro.git
cd rhf-act-bug-repro
yarn install
yarn test
--> See that the tests pass without any act warnings.git checkout user-event@14
yarn install
yarn test
--> See that the tests pass but now with an act warning.Expected behavior
I expect that the test below passes without any act warnings.
Actual behavior
The following test produces an act warning.
Wrapping the
await userEvent.clear(screen.getByLabelText("Title"))
in anact
removes the warning.This works as expected with
@testing-library/user-event@13.2.1
but breaks in@testing-library/user-event@14.1.1
User-event version
14.1.1
Environment
Testing Library framework:
@testing-library/react@13.0.0
JS framework:
react@18.0.0
andreact-hook-form@7.30.0
Test environment:
react-scripts@5.0.1
which usesjest@27.5.1
Additional context
Working version on
main
branch of repro is@testing-library/user-event@13.2.1
The text was updated successfully, but these errors were encountered: