简体   繁体   中英

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:

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?

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: {
  enableGlobally: true,
},

Then I removed the individual jest.useFakeTimers(); from my specs. This got rid of the TypeError: Cannot read property 'isFake' of undefined error.

Reference: https://jestjs.io/docs/28.x/upgrading-to-jest28#fake-timers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM