Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 718
berc123 Avatar asked Sep 08 '25 03:09

berc123


1 Answers

I was able to resolve this issue using legacy fake timers.

jest.useFakeTimers({ legacyFakeTimers: true });
like image 80
h0b0 Avatar answered Sep 09 '25 23:09

h0b0