简体   繁体   English

Jest 抛出类型错误:无法读取未定义的属性(读取“isFake”)

[英]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.

相关问题 类型错误:无法读取未定义的属性(读取“拆分”)” - TypeError: Cannot read properties of undefined (reading 'split')" TypeError:无法读取未定义的属性(读取“baseURL”) - TypeError: Cannot read properties of undefined (reading 'baseURL') TypeError:无法读取未定义的属性(读取“事件”) - TypeError: Cannot read properties of undefined (reading 'event') TypeError:无法读取未定义的属性(读取“authService”) - TypeError: Cannot read properties of undefined (reading 'authService') 类型错误:无法读取未定义的属性(读取“ngModule”) - TypeError: Cannot read properties of undefined (reading 'ngModule') 类型错误:无法读取未定义的属性(读取 &#39;<myvariable> &#39;) - TypeError: Cannot read properties of undefined (reading '<myvariable>') TypeError:无法读取未定义的属性(读取“getDeep”) - TypeError: Cannot read properties of undefined (reading 'getDeep') TypeError:无法读取未定义的属性(读取“管道”) - TypeError: Cannot read properties of undefined (reading 'pipe') TypeError:无法读取未定义的属性(读取“拆分”) - TypeError: cannot read properties of undefined (reading 'split') 类型错误:无法读取未定义的属性(读取“paramMap”) - TypeError: Cannot read properties of undefined (reading 'paramMap')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM