-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Vitest + act raising warnings #4640
Comments
@BreakBB describe('App', () => {
it('should allow act', async () => {
render(<App />);
// await act(async () => {
await userEvent.type(screen.getByRole('textbox'), '1');
// });
expect(screen.getByRole('textbox')).toHaveValue('03/26/20241');
});
it('should allow act without date picker', async () => {
render(<App />);
await act(async () => {
await userEvent.click(screen.getByText(/count is 0/i));
});
expect(screen.getByText(/count is 1/i)).toBeDefined();
});
}); |
Thanks for your response @yuki0410-dev You are right, that the test will stay green without a warning, when removing the However in a more complex setup, the I am confused this warning is only raised when interacting with the datepicker. Not with a default |
@BreakBB Thanks for the reply. Since there is a similar issue in the RTL issue, I suspect that it is not a react-datepicker-specific issue, but rather occurs when testing a component that is updating the state internally. (asynchronously?). testing-library/user-event#1104 It may also occur when combining act and userEvent. |
I also think it is related to this. The posts you linked (except for the Japanese one) are the sources I also found and which I tried to match on the behavior of react-datepicker. In my actual test setup, I am facing the issue, that I can not wait for any UI element to update, because the warning is raised while typing to the date input. I'll try to produce an example where the datepicker raises |
I don't know if this will help, but I encountered a similar case in this Repository test, but I got around it by putting expect in waitFor. |
We finally managed to work around these issues and were also able further pin the actual issue down. In our actual app (not the repo I linked above), we were typing a date and then used const DeliveryDateInput = forwardRef(({ hasError, ...rest }: { hasError: boolean }, ref) => (
<Tooltip title={hasError ? "Please enter a delivery date" : ""}>
<TextField {...rest} inputRef={ref} label="Delivery Date" size="small" error={hasError} />
</Tooltip>
)); This However, when removing the Now to work around the warnings, we changed the things we tested and no longer use So from my side this issue can be closed as we no longer have any act-warnings. Even though we didn't manage to fully understand what is causing the issue. |
this is special solution for datepicker from @mui, maybe work good for other case: // old code:
// import { act } from "@testing-library/react"
// new code:
import { waitFor } from "@testing-library/react"
// old code:
//await act(async () => {
// await userEvent.click(screen.getByText(/count is 0/i));
//});
// new code:
await waitFor(async () => {
await userEvent.click(screen.getByText(/count is 0/i));
}); |
This is not a solution @commanderz - This is a workaround, because it will just cover the issue by an additional wait. Using |
I encountered the same issue, and it only appeared when using However, this workaround doesn’t seem ideal. I’m still unsure why this discrepancy arises, but it seems tied to how the state is managed asynchronously within the datepicker component. |
After the latest update to The export const DeliveryDateFormField = ({
name,
methods,
required,
hasError,
}: Props) => (
<Controller
name={name}
control={methods.control}
rules={{
validate: value => (required ? !!value && isFutureDateAndTime(value) : true),
}}
render={({ field: { onChange, onBlur, value } }) => {
const selectedDate = new Date(value);
return (
<StyledDatePicker
selected={!value ? null : selectedDate}
onChange={onChange}
onBlur={onBlur}
locale="de"
dateFormat="dd.MM.yyyy HH:mm"
customInput={<DeliveryDateInput hasError={hasError} />}
showTimeSelect
timeIntervals={15}
minDate={new Date()}
minTime={getMinTime(name, selectedDate)}
maxTime={new Date(0, 0, 0, 18, 0)}
timeCaption={"Uhrzeit"}
sx={{
height: "2.5rem",
border: "none",
width: "100%",
marginBottom: "0.75rem",
}}
/>
);
}}
/>
); I got rid of my - const DeliveryDateInput = forwardRef(({ hasError, ...rest }: { hasError: boolean }, ref) => (
+ const DeliveryDateInput = forwardRef(({ hasError, ...rest }: { hasError: boolean }) => (
<Tooltip title={hasError ? "Please enter a delivery date" : ""}>
- <TextField {...rest} inputRef={ref} label="Delivery Date" size="small" error={hasError} />
+ <TextField {...rest} label="Liefertermin" size="small" error={hasError} />
</Tooltip>
)); In my head this makes sense, because by wrapping the DatePicker in a form-control, I bind these together and want the form to take care of handling the change events and stuff. But when handing the |
Describe the bug
When running tests using
act
that interact withreact-datepicker
a warning is raised saying:This warning does not appear when not interacting with the datepicker.
To Reproduce
Steps to reproduce the behavior:
I tried to created a minimal reproducible example in this repo: https://github.com/BreakBB/act-test
So clone the repo, run
npm ci
and run the two tests inApp.test.js
:Expected behavior
I want both tests to continue passing, but without the warning message.
Dependencies
The text was updated successfully, but these errors were encountered: