CodePark/ questions / User interactions
START PRACTICINGPRACTICE
Frontend question · Testing › User interactions

Which pattern for setting up user-event does its documentation recommend?

function setup(jsx: React.ReactElement) {
  return { user: userEvent.setup(), ...render(jsx) };
}
Call userEvent.setup() inside each test, or in a setup() helper that the test calls, before rendering

The docs recommend calling setup() inside each test, often through a small helper that returns { user, ...render(jsx) }. The beforeEach option is tempting but wrong. The docs discourage rendering or using user-event in before or after hooks. The module-level option is wrong too, because one shared instance leaks state between tests.