react: Bug: using MessageChannel prevents node.js process from exiting
Edited: Here’s how to fix this: https://github.com/facebook/react/issues/20756#issuecomment-780927519.
React version: 17.0.1 (latest)
Steps To Reproduce
This code finishes properly in node.js 14, but holds the process open in node.js 15
global.window = global; // simulate JSDOM
require('scheduler');
Since node.js 15, there is a global MessageChannel object now, which prevents node event loop from exiting. To let the process shut down properly, it should either call port.close() or port.unref()
This becomes an issue when testing React code within JSDOM environment, as the test process cannot exit properly.
The current behavior
The process with JSDOM tests never finishes
The expected behavior
The process finishes properly
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Commits related to this issue
- tests: fix tests not ending (react bug) More info: https://github.com/facebook/react/issues/20756#issuecomment-780927519 — committed to rithvikvibhu/bob-wallet by rithvikvibhu 2 years ago
- tests: webpack config and react bug More info: https://github.com/facebook/react/issues/20756#issuecomment-780927519 — committed to rithvikvibhu/bob-wallet by rithvikvibhu 2 years ago
- tests: webpack config and react bug More info: https://github.com/facebook/react/issues/20756#issuecomment-780927519 — committed to pinheadmz/bob-wallet by rithvikvibhu 2 years ago
- Update dependencies and support darwin-arm64 (Apple M1) (#444) * pkg: update dependencies webpack/babel/electron * webpack: update configs to migrate to webpack 5 * pkg: update babel config for... — committed to kyokan/bob-wallet by pinheadmz 2 years ago
- tests: webpack config and react bug More info: https://github.com/facebook/react/issues/20756#issuecomment-780927519 — committed to lukeburns/bob-wallet by rithvikvibhu 2 years ago
- @0.11.1, fix node 15+ hang in jsdom - https://github.com/facebook/react/issues/20756 — committed to localnerve/element-size-reporter by localnerve 2 years ago
- Upgrade `eval` again, it's a React bug. https://github.com/facebook/react/issues/20756#issuecomment-780927519 again — committed to Automattic/jetpack by anomiex 2 years ago
- fix react render hanging test https://github.com/facebook/react/issues/20756 — committed to software-mansion/react-native-reanimated by tjzel a year ago
- Rewrite jest unit tests to TypeScript (#4399) ## Summary - Renamed all tests with their respective TypeScript counterparts. - Fixed jestUtils to properly allow strict checking of AnimatedStyle. - Ad... — committed to software-mansion/react-native-reanimated by tjzel a year ago
- Fix/yarn test (#6681) This is to fix yarn test. This simply adds new scripts through the yarn workspace command to run app and lib tests. Currently, the lib testing works by mainly using the same sc... — committed to streamlit/streamlit by willhuang1997 a year ago
- Rewrite jest unit tests to TypeScript (#4399) ## Summary - Renamed all tests with their respective TypeScript counterparts. - Fixed jestUtils to properly allow strict checking of AnimatedStyle. - Ad... — committed to wordpress-mobile/react-native-reanimated by tjzel a year ago
- Feature/st lib (#6692) * Minimal exports for app to consume + VerticalBlock and ElementNodeRenderer (#6623) This PR is the first milestone to determine the minimal number of exports needed for app... — committed to streamlit/streamlit by willhuang1997 a year ago
- test: Fix open handles to improve test lisibility We have a lot of open handles flooding the end of our test results. It seems that they are due to a React issue fixed in 18+. Dan Abramov wrote a sm... — committed to cozy/cozy-flagship-app by zatteo 7 months ago
- test: Fix open handles to improve test lisibility We have a lot of open handles flooding the end of our test results. It seems that they are due to a React issue fixed in 18+. Dan Abramov wrote a sm... — committed to cozy/cozy-flagship-app by zatteo 7 months ago
- test: Fix open handles to improve test lisibility We have a lot of open handles flooding the end of our test results. It seems that they are due to a React issue fixed in 18+. Dan Abramov wrote a sm... — committed to cozy/cozy-flagship-app by zatteo 7 months ago
- test: Fix open handles to improve test lisibility We have a lot of open handles flooding the end of our test results. It seems that they are due to a React issue fixed in 18+. Dan Abramov wrote a sm... — committed to cozy/cozy-flagship-app by zatteo 7 months ago
- Feature/st lib (#6692) * Minimal exports for app to consume + VerticalBlock and ElementNodeRenderer (#6623) This PR is the first milestone to determine the minimal number of exports needed for app... — committed to eric-skydio/streamlit by willhuang1997 a year ago
- Feature/st lib (#6692) * Minimal exports for app to consume + VerticalBlock and ElementNodeRenderer (#6623) This PR is the first milestone to determine the minimal number of exports needed for app... — committed to zyxue/streamlit by willhuang1997 a year ago
How to fix this
If you can upgrade to React 17.1.0
When React 17.1.0 comes out, it will contain a fix for this. So upgrading would solve this problem.
(At the time this comment is written, it’s not out yet, but I expect it to be out soon.)
If you can’t upgrade to React 17.1.0
and add this to your test setup file as THE VERY FIRST IMPORT:
(For example, Create React App projects use
src/setupTests.jsas the test setup file, otherwise you’ll need to refer to your test runner’s documentation. In Jest, the option is calledsetupFiles).This should fix the hanging on Node 15+ with jsdom on older versions of React. We don’t plan to be updating them because there’s too much risk of breaking something else, as this detection is subtle and there are many possible edge cases.
The package with the fix will print a warning asking you to remove it after you upgrade to React 17.1.0. This is because the fix itself is to disable Node’s
MessageChannelimplementation, which is not ideal in long term.Let me know if you run into more problems! I’ll keep the issue for now until we release 17.1.0.
Brilliant fix 😃
Would it make sense to let Node.js devs know that this is happening? Perhaps, they could consider undoing the change in Node 16.
For now I’ve just kept using the temp fix package but patched it to adjust the version on which it applies the warning. I’m hoping we can update storybook soon and so won’t need to make any adjustments after that.
On Fri, Sep 9, 2022, 7:12 AM Andre Rosa @.***> wrote:
@rickhanlonii – ah, so we’re using
20.0.2forreact-dom, but one of ourstorybook-related dependencies is bringing in0.19.1as a transitive ofreact-dom@16.14.0. I assume that may be the issue I’ll see if I can get that bumped or excluded from executing in our tests.Also… it looks like maybe the fix actually didn’t land until React 18, so maybe needs to be changed to
18.0.0?Ref: Mention of the fixing PR (#20834) in https://github.com/facebook/react/pull/24195.
@gaearon – I think the
react-16-node-hanging-test-fixfile may have a critical typo:Shouldn’t the
ifbe evaluating for “greater than or equal to 17.1.0”? Per your comment and the comment at the top of the package file, the issue is fixed starting with17.1.0.Right now it’s evaluating “greater than 17.0.1” – so I’m getting the warning for
17.0.2even though the issue isn’t fixed there.We’re going with https://github.com/facebook/react/pull/20834 as a likely fix in 17.1.0, but it will need more testing.