[英]Jest throws TypeError: Cannot read properties of undefined (reading 'isFake')
When I run in NX monorepo jest unit tests, In some tests (not in all test files) jest >= 28 throws the error:当我在 NX monorepo jest 单元测试中运行时,在某些测试中(不是在所有测试文件中)jest >= 28 抛出错误:
TypeError: Cannot read properties of undefined (reading 'isFake')
150 |
151 | beforeEach(() => {
> 152 | jest.useFakeTimers();
what is an issue in the fake-timers lib on the line: if (_global.Date.isFake === true) {
线上的假计时器库中有什么问题:
if (_global.Date.isFake === true) {
When I downgraded jest to version 27.5.1 all tests are passing.当我将 jest 降级到版本 27.5.1 时,所有测试都通过了。 Seems like issue in initialisation of the tests.
似乎是测试初始化的问题。
What could be the issue?可能是什么问题?
I was able to resolve this issue using legacy fake timers.我能够使用遗留的假计时器解决这个问题。
jest.useFakeTimers({ legacyFakeTimers: true });
In my case, I just solved this by using the fakeTimers
Jest config, like so:就我而言,我只是通过使用
fakeTimers
Jest 配置解决了这个问题,如下所示:
fakeTimers: {
enableGlobally: true,
},
Then I removed the individual jest.useFakeTimers();
然后我删除了个人
jest.useFakeTimers();
from my specs.从我的规格。 This got rid of the
TypeError: Cannot read property 'isFake' of undefined
error.这摆脱了
TypeError: Cannot read property 'isFake' of undefined
错误。
Reference: https://jestjs.io/docs/28.x/upgrading-to-jest28#fake-timers参考: https://jestjs.io/docs/28.x/upgrading-to-jest28#fake-timers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.