最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot assign to read only property 'performance' of object '[object global]&

programmeradmin1浏览0评论

Error:

This error was only popping up for me when running CI for jest. I could not catch it locally in any way. Error text:

FAIL src/utils/message.test.ts
  ● Test suite failed to run

    TypeError: Cannot assign to read only property 'performance' of object '[object global]'

       5 |
       6 | jest
    >  7 |   .useFakeTimers({ legacyFakeTimers: false })
         |    ^
       8 |   .setSystemTime(new Date(fakeTime));
       9 |
      10 | jest.mock('src/constants/env', () => {

      at hijackMethod (../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:946:32)
      at Object.install (../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1733:17)
      at Object.<anonymous> (src/utils/message.test.ts:7:4)

Error:

This error was only popping up for me when running CI for jest. I could not catch it locally in any way. Error text:

FAIL src/utils/message.test.ts
  ● Test suite failed to run

    TypeError: Cannot assign to read only property 'performance' of object '[object global]'

       5 |
       6 | jest
    >  7 |   .useFakeTimers({ legacyFakeTimers: false })
         |    ^
       8 |   .setSystemTime(new Date(fakeTime));
       9 |
      10 | jest.mock('src/constants/env', () => {

      at hijackMethod (../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:946:32)
      at Object.install (../node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1733:17)
      at Object.<anonymous> (src/utils/message.test.ts:7:4)
Share Improve this question asked Dec 20, 2023 at 23:17 Илья ХоришкоИлья Хоришко 1,1951 gold badge10 silver badges22 bronze badges 2
  • Very similar question and answer here ~ TypeError: Cannot assign to read only property 'now' of object '#<Performance>' – Phil Commented Dec 20, 2023 at 23:33
  • @Phil Unfortunately I only found the answer after 4 hours of searching. On github, so I decided to make this post – Илья Хоришко Commented Dec 20, 2023 at 23:37
Add a comment  | 

3 Answers 3

Reset to default 14

Solution

In src/utils/message.test.ts (your test file) use it before useFakeTimers

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

Result code

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

jest
  .useFakeTimers({ legacyFakeTimers: false })
  .setSystemTime(new Date(fakeTime));

Thanks

https://github.com/facebook/react-native/issues/35701#issuecomment-1847579429

Upgrading to Jest v.29.6.4 worked for me (see https://github.com/facebook/react-native/issues/35701#issuecomment-1697798232).

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

in

react-native/jest/setup.js

 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论