Jest.js TypeError:无法为对象“[object global]”的只读属性“performance”赋值

dbf7pr2w  于 11个月前  发布在  Jest
关注(0)|答案(1)|浏览(146)

错误:

这个错误只在我运行jest的CI时弹出。我无法以任何方式在本地捕获它。错误文本:

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)

字符串

w80xi6nr

w80xi6nr1#

解决方案

src/utils/message.test.ts(您的测试文件)中,在useFakeTimers之前使用它

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

字符串

结果代码

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

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


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

相关问题