Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to read only property 'performance' of object '[object global]' #35701

Open
Romick2005 opened this issue Dec 21, 2022 · 19 comments
Labels
Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@Romick2005
Copy link
Contributor

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)

image

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})

@mogelbrod
Copy link

I got this error after upgrading to node v19. Changing these lines

global.performance = {
now: jest.fn(Date.now),
};

to something like

if (!global.performance) {
  global.performance = {}
}
global.performance.now = jest.fn(Date.now)

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.

@Anton-Petrovskyi
Copy link

Anton-Petrovskyi commented Jan 23, 2023

The problem is in node version indeed.
I was using stable node version on my project and I had node installed with homebrew (that is system node version).
NVM prioritizes system-node over default alias, so jest was using node v19 that triggers this error.
I had to remove system node version. Now jest is running on stable node version

FIX:
brew uninstall node
nvm use stable

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 23, 2023
@mogelbrod
Copy link

Still relevant

@github-actions github-actions bot added the Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. label Jul 23, 2023
@github-actions
Copy link

⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 24, 2023
@rudda-involves
Copy link

is there a way to override it outside node_modules/react-native/jest/setup.js file?

@rudda
Copy link

rudda commented Aug 29, 2023

solved in jest 29.6.4 version

@reporter123
Copy link

reporter123 commented Aug 29, 2023

looks like jest 29.2.1 is minimum for this fix https://github.com/jestjs/jest/blob/main/CHANGELOG.md

29.6.4 contains additional fixes for node 20.

@mrliptontea
Copy link

mrliptontea commented Dec 4, 2023

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:

$ docker run --rm -it node:18.18 node 
Welcome to Node.js v18.18.2.
Type ".help" for more information.
> Object.getOwnPropertyDescriptor(globalThis, 'performance');
{
  value: Performance {
    nodeTiming: PerformanceNodeTiming {
      name: 'node',
      entryType: 'node',
      startTime: 0,
      duration: 1273.0532480003312,
      nodeStart: 1.4266739999875426,
      v8Start: 6.116002000402659,
      bootstrapComplete: 25.822167000267655,
      environment: 13.395024999976158,
      loopStart: 42.115690000355244,
      loopExit: -1,
      idleTime: 1205.711491
    },
    timeOrigin: 1701690439884.343
  },
  writable: true,
  enumerable: true,
  configurable: true
}
$ docker run --rm -it node:18.19 node
Welcome to Node.js v18.19.0.
Type ".help" for more information.
> Object.getOwnPropertyDescriptor(globalThis, 'performance');
{
  get: [Function: get performance],
  set: [Function: set performance],
  enumerable: true,
  configurable: true
}

@antonio-ivanovski
Copy link

solved in jest 29.6.4 version

For me this is still not being solved, using jest 29.7.0

@leograde
Copy link

leograde commented Dec 8, 2023

Also not solved for me on 29.7.0 and node v20.3.0

@manattan
Copy link

manattan commented Dec 8, 2023

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?

Object.defineProperty(global, "performance", {
  writable: true,
});

(Admittedly, modifying the global object easily may not be a good practice.)

cortisiko pushed a commit to MetaMask/metamask-mobile that referenced this issue Dec 8, 2023
…obal.performance (#8046)

## **Description**

Fix recent CI flakey failing unit tests:
`TypeError: Cannot assign to read only property 'performance' of object
'[object global]'`

Failing at:
https://github.com/facebook/react-native/blob/v0.71.14/jest/setup.js#L20-L22
_Nb: https://github.com/facebook/react-native/blob/v0.72.0/jest/setup.js
404 file no longer exists in this future RN ver_

Reconsider current node/jest/RN vers we're using (see
[issue](facebook/react-native#35701) and
[comment](#8046 (comment))
below)

> it broke between node 18.18 and 18.19 - [Jest relies on
descriptor.writable](https://github.com/jestjs/jest/blob/v28.1.3/packages/jest-environment-node/src/index.ts#L88)
but it's not longer returned in the descriptor

Nb: Node 18.19
[changelog](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V18.md#2023-11-29-version-18190-hydrogen-lts-targos)

## **Related issues**

Fixes: #8051

## **Manual testing steps**

Run unit tests

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
@juanito-caylent
Copy link

Worth linking this here: nodejs/node#51048 (this same issue being reported directly to Node).

sethkfman pushed a commit to MetaMask/metamask-mobile that referenced this issue Dec 8, 2023
…obal.performance (#8046)

## **Description**

Fix recent CI flakey failing unit tests:
`TypeError: Cannot assign to read only property 'performance' of object
'[object global]'`

Failing at:
https://github.com/facebook/react-native/blob/v0.71.14/jest/setup.js#L20-L22
_Nb: https://github.com/facebook/react-native/blob/v0.72.0/jest/setup.js
404 file no longer exists in this future RN ver_

Reconsider current node/jest/RN vers we're using (see
[issue](facebook/react-native#35701) and
[comment](#8046 (comment))
below)

> it broke between node 18.18 and 18.19 - [Jest relies on
descriptor.writable](https://github.com/jestjs/jest/blob/v28.1.3/packages/jest-environment-node/src/index.ts#L88)
but it's not longer returned in the descriptor

Nb: Node 18.19
[changelog](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V18.md#2023-11-29-version-18190-hydrogen-lts-targos)

## **Related issues**

Fixes: #8051

## **Manual testing steps**

Run unit tests

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
@carlmlane
Copy link

pinning to node version 18.18 was the only way for me to workaround this issue

gantunesr pushed a commit to MetaMask/metamask-mobile that referenced this issue Dec 14, 2023
…obal.performance (#8046)

## **Description**

Fix recent CI flakey failing unit tests:
`TypeError: Cannot assign to read only property 'performance' of object
'[object global]'`

Failing at:
https://github.com/facebook/react-native/blob/v0.71.14/jest/setup.js#L20-L22
_Nb: https://github.com/facebook/react-native/blob/v0.72.0/jest/setup.js
404 file no longer exists in this future RN ver_

Reconsider current node/jest/RN vers we're using (see
[issue](facebook/react-native#35701) and
[comment](#8046 (comment))
below)

> it broke between node 18.18 and 18.19 - [Jest relies on
descriptor.writable](https://github.com/jestjs/jest/blob/v28.1.3/packages/jest-environment-node/src/index.ts#L88)
but it's not longer returned in the descriptor

Nb: Node 18.19
[changelog](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V18.md#2023-11-29-version-18190-hydrogen-lts-targos)

## **Related issues**

Fixes: #8051

## **Manual testing steps**

Run unit tests

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
gantunesr pushed a commit to MetaMask/metamask-mobile that referenced this issue Dec 14, 2023
…obal.performance (#8046)

## **Description**

Fix recent CI flakey failing unit tests:
`TypeError: Cannot assign to read only property 'performance' of object
'[object global]'`

Failing at:
https://github.com/facebook/react-native/blob/v0.71.14/jest/setup.js#L20-L22
_Nb: https://github.com/facebook/react-native/blob/v0.72.0/jest/setup.js
404 file no longer exists in this future RN ver_

Reconsider current node/jest/RN vers we're using (see
[issue](facebook/react-native#35701) and
[comment](#8046 (comment))
below)

> it broke between node 18.18 and 18.19 - [Jest relies on
descriptor.writable](https://github.com/jestjs/jest/blob/v28.1.3/packages/jest-environment-node/src/index.ts#L88)
but it's not longer returned in the descriptor

Nb: Node 18.19
[changelog](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V18.md#2023-11-29-version-18190-hydrogen-lts-targos)

## **Related issues**

Fixes: #8051

## **Manual testing steps**

Run unit tests

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
gantunesr pushed a commit to MetaMask/metamask-mobile that referenced this issue Dec 14, 2023
…obal.performance (#8046)

## **Description**

Fix recent CI flakey failing unit tests:
`TypeError: Cannot assign to read only property 'performance' of object
'[object global]'`

Failing at:
https://github.com/facebook/react-native/blob/v0.71.14/jest/setup.js#L20-L22
_Nb: https://github.com/facebook/react-native/blob/v0.72.0/jest/setup.js
404 file no longer exists in this future RN ver_

Reconsider current node/jest/RN vers we're using (see
[issue](facebook/react-native#35701) and
[comment](#8046 (comment))
below)

> it broke between node 18.18 and 18.19 - [Jest relies on
descriptor.writable](https://github.com/jestjs/jest/blob/v28.1.3/packages/jest-environment-node/src/index.ts#L88)
but it's not longer returned in the descriptor

Nb: Node 18.19
[changelog](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V18.md#2023-11-29-version-18190-hydrogen-lts-targos)

## **Related issues**

Fixes: #8051

## **Manual testing steps**

Run unit tests

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
kdy1 added a commit to swc-project/swc-ecosystem-ci that referenced this issue Dec 18, 2023
@mrvincenzo
Copy link

mrvincenzo commented Jan 9, 2024

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?

Object.defineProperty(global, "performance", {
  writable: true,
});

(Admittedly, modifying the global object easily may not be a good practice.)

@manattan where did you put it?

@mrvincenzo
Copy link

mrvincenzo commented Jan 10, 2024

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?

Object.defineProperty(global, "performance", {
  writable: true,
});

(Admittedly, modifying the global object easily may not be a good practice.)

@manattan where did you put it?

I ended up with this patch in react-native/jest/setup.js:

Replaced this code

...
global.performance = {
    now: jest.fn(Date.now),
};

with this code

...
Object.defineProperty(global, "performance", {
  writable: true,
});
...

Node: 21.5.0
Jest: 29.7.0
React Native: 0.71.13

UPDATE:

My initial patch caused a different error on some machines

TypeError: Cannot read properties of undefined (reading 'now')

So I (with the help of ChatGPT) refactored the code to

const performanceDescriptor = Object.getOwnPropertyDescriptor(global, 'performance');
if (!performanceDescriptor || performanceDescriptor.configurable) {
  Object.defineProperty(global, 'performance', {
    value: {
      now: jest.fn(Date.now),
    },
    writable: true,
  });
} else {
  global.performance.now = jest.fn(Date.now);
}

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

Copy link

github-actions bot commented Jul 9, 2024

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 9, 2024
@soryy708
Copy link

What helped me is upgrading from Jest 28 to Jest 29

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 15, 2024
@react-native-bot
Copy link
Collaborator

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@react-native-bot react-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests