错误:
这个错误只在我运行 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/