i’m using react-aria and react testing-library.
In my Button component, i’m using react-aria’s useButton
and I use onPress
prop to pass the function to call once click. In the unit test, I just try to click it in order to trigger a simple jest.fn() function, and all I do after that is check if function have been called.
const handleOnClick = jest.fn();
render(<Button onPress={handleOnClick}>test me</Button>);
userEvent.click(screen.getByRole('button'));
expect(handleOnClick).toHaveBeenCalled();
That is showing the following warning:
console.error
Warning: An update to Button inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
If I wrap the click with act the warning is gone, but seems it’s not a good practice to do that, because, in fact, testing library is doing an act internally in userEvent
and in other methods. What can I do? Thanks.