Jest抛出类型错误:无法读取undefined的属性(阅读'isFake')

c3frrgcw  于 2022-12-08  发布在  Jest
关注(0)|答案(2)|浏览(387)

When I run in NX monorepo jest unit tests, In some tests (not in all test files) jest >= 28 throws the error:

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) {
When I downgraded jest to version 27.5.1 all tests are passing. Seems like issue in initialisation of the tests.
What could be the issue ?

hrirmatl

hrirmatl1#

我能够解决这个问题使用传统的假计时器。

jest.useFakeTimers({ legacyFakeTimers: true });
w7t8yxp5

w7t8yxp52#

在我的例子中,我只是通过使用fakeTimers Jest配置解决了这个问题,如下所示:

fakeTimers: {
  enableGlobally: true,
},

然后我从我的规格中删除了单个的jest.useFakeTimers();。这就消除了TypeError: Cannot read property 'isFake' of undefined错误。
参考:https://jestjs.io/docs/28.x/upgrading-to-jest28#fake-timers

相关问题