facebook/react

Bug: There is no re-render on DOM events in Node.js test runner

krutoo opened this issue · 1 comments

Hi, I tried to write tests with Node.js test runner with jsdom but this is not working.

Initial render and mount works fine.

But when event is dispatched (click for example) there is no re-render of component.

React version: 18.3.1

Guides I used

Steps To Reproduce

  1. Have Node.js 20+
  2. Clone repository https://github.com/krutoo/node-test-runner-react-dom
  3. npm i
  4. npm run test

Link to code example: https://github.com/krutoo/node-test-runner-react-dom

The current behavior

There is no re-render of component after event is dispatched and as result test is failed

The expected behavior

Test is done successfully

Found an answer

The reason why test is failed in repo is because in test environment React does not handles element.dispatchEvent(new Event('something'))

To dispach event in test environment you need something like @testing-library/react

Thoughts

I think this is weird because there is no clear explanation why this is like that

In old React site there is some information about how to test components

In new site https://react.dev/ there is absolutely no explanation about why is working like that, there is just "You need something like testing library"