javascript - 类型错误 : Cannot assign to read only property 'performance' of object '[object global]'

标签 javascript node.js typescript jestjs usefaketimers

错误:

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

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)

最佳答案

解决方案

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

关于javascript - 类型错误 : Cannot assign to read only property 'performance' of object '[object global]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77694957/

相关文章:

javascript - three.js 的 Matrix4.multiply() 方法有什么作用?

angular - 在 Ionic 2 中隐藏子页面中的选项卡

javascript - 三JS : IE11 crashes on Render();

javascript - 使用javascript删除部分字符串

javascript - 从数组获取非重复索引在 0 处失败

node.js - 由于虚拟目录,npm 安装失败

node.js - req.user undefined - node + express + passport-facebook

javascript - 在 Node.js 中完成 for 循环后如何运行函数?

angular - 我得到这个 ' Property ' 匹配'在类型 'EventTarget' 上不存在。'在 Angular-cli 输出上

typescript - Cypress cucumber 预处理器自定义参数类型与 typescript