react-native: Cannot assign to read only property 'performance' of object '[object global]'
Description
FAIL src/Containers/Contacts/tests/Contacts.test.js ● Test suite failed to run
TypeError: Cannot assign to read only property 'performance' of object '[object global]'
at Object.<anonymous> (node_modules/react-native/jest/setup.js:405:20)
Version
0.68.5
Output of npx react-native info
System: OS: macOS 13.0.1 CPU: (8) arm64 Apple M1 Memory: 781.28 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 19.3.0 - /opt/homebrew/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 9.2.0 - /opt/homebrew/bin/npm Watchman: 2022.12.12.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /Users/user/.rbenv/shims/pod SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: Not Found IDEs: Android Studio: 2021.3 AI-213.7172.25.2113.9014738 Xcode: 14.2/14C18 - /usr/bin/xcodebuild Languages: Java: 11.0.16.1 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.68.5 => 0.68.5 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found
Steps to reproduce
Run nvm use yarn test
.
It works for node 18.12.1
, but not for node 19.3.0
.
Snack, code example, screenshot, or link to a repository
Works on lts/hydrogen -> v18.12.1. But because node 19 decides to make global performance property as read only ({writable: false}
)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 16
- Comments: 16
Commits related to this issue
- fix: CI flakey unit tests TypeErrors on react-native/jest/setup.js global.performance (#8046) ## **Description** Fix recent CI flakey failing unit tests: `TypeError: Cannot assign to read only pr... — committed to MetaMask/metamask-mobile by leotm 7 months ago
- fix: CI flakey unit tests TypeErrors on react-native/jest/setup.js global.performance (#8046) ## **Description** Fix recent CI flakey failing unit tests: `TypeError: Cannot assign to read only pr... — committed to MetaMask/metamask-mobile by leotm 7 months ago
- fix: CI flakey unit tests TypeErrors on react-native/jest/setup.js global.performance (#8046) ## **Description** Fix recent CI flakey failing unit tests: `TypeError: Cannot assign to read only pr... — committed to MetaMask/metamask-mobile by leotm 7 months ago
- fix: CI flakey unit tests TypeErrors on react-native/jest/setup.js global.performance (#8046) ## **Description** Fix recent CI flakey failing unit tests: `TypeError: Cannot assign to read only pr... — committed to MetaMask/metamask-mobile by leotm 7 months ago
- fix: CI flakey unit tests TypeErrors on react-native/jest/setup.js global.performance (#8046) ## **Description** Fix recent CI flakey failing unit tests: `TypeError: Cannot assign to read only pr... — committed to MetaMask/metamask-mobile by leotm 7 months ago
- Override jest to workaround https://github.com/facebook/react-native/issues/35701 — committed to swc-project/swc-ecosystem-ci by kdy1 6 months ago
The problem is in node version indeed. I was using
stable
node version on my project and I had node installed with homebrew (that issystem
node version). NVM prioritizessystem-node
overdefault
alias, so jest was using node v19 that triggers this error. I had to removesystem
node version. Now jest is running onstable
node versionFIX:
brew uninstall node
nvm use stable
For me this is still not being solved, using jest 29.7.0
I got this error after upgrading to node v19. Changing these lines https://github.com/facebook/react-native/blob/473eb1dd870a4f62c4ebcba27e12bde1e99e3d07/jest/setup.js#L20-L22 to something like
Might solve the issue. I’m guessing that node v18/19 started defining
performance
or added a guard that prevents reassigning it. Couldn’t find anything related in the release notes though.Hello.
I encountered this error when running Jest in a Node.js project (not a React Native project).
If the issue is that the global object’s performance is read-only, it seems like the following explicit declaration could resolve it (at least it worked for me). However, is there anything problematic with this?
(Admittedly, modifying the global object easily may not be a good practice.)
Worth linking this here: https://github.com/nodejs/node/issues/51048 (this same issue being reported directly to Node).
I’ve run into this on a project using Jest and this is the only issue I could find with the exact error I was seeing - it broke between node 18.18 and 18.19 - Jest relies on
descriptor.writable
but it’s not longer returned in the descriptor:I ended up with this patch in
react-native/jest/setup.js
:Replaced this code
with this code
Node: 21.5.0 Jest: 29.7.0 React Native: 0.71.13
UPDATE:
My initial patch caused a different error on some machines
So I (with the help of ChatGPT) refactored the code to
which seems to be working fine on all machines with this setup: Node: 21.5.0 Jest: 29.7.0 React Native: 0.71.13
solved in jest
29.6.4
version