sinonjs / fake-timers

Fake setTimeout and friends (collectively known as "timers"). Useful in your JavaScript tests. Extracted from Sinon.JS
BSD 3-Clause "New" or "Revised" License
793 stars 103 forks source link

TypeError Since Node 19 #463

Closed charlieyeng closed 1 year ago

charlieyeng commented 1 year ago

We understand you have a problem and are in a hurry, but please provide us with some info to make it much more likely for your issue to be understood, worked on and resolved quickly.

What did you expect to happen? I expect the jest.useFakeTimers() function to work out of the box. What actually happens It works for Node v18, but upgrading my stack to Node v19 and beyond we run into the following errors. TypeError: Cannot assign to read only property 'performance' of object '[object global]' and Can't install fake timers twice on the same global object. jest.useFakeTimers() is backed by @sinon/fake-timers Jest-Reference How to reproduce Have the latest node and jest versions installed

brew install node 
npm install --save-dev jest

and attempt to call `jest.useFakeTimers() ie:

jest.useFakeTimers()

will result in failures.

Similar issues noted here: Bug and perhaps related to this

musojames commented 1 year ago

I've run into the same issue with node 19.0.1 on Mac OS Monterrey 12.6

mrazauskas commented 1 year ago

How to reproduce Have the latest node and jest versions installed

Above you mention that "@jest/fake-timers": "^28.1.3" is installed. The latest Jest version v29.5.0.

Which one did you actually use? The latest one or v28? Did you try upgrading to the latest one?


If upgrading does not help, since Jest v28 you can excluding performance from the list of faked APIs: jest.useFakeTimers({doNotFake: ['performance']});

Reference: https://github.com/jestjs/jest/issues/12055#issuecomment-1000415161

fatso83 commented 1 year ago

@charlieyeng Reproducible with Jest 28. Not reproducible with Jest 29 and Node 19. This means you need to upgrade your Jest version.

$ cat test/foo.test.js

test('hey', () => {
   jest.useFakeTimers()
})

$ npx jest test/foo.test.js
 PASS  test/foo.test.js
  ✓ hey

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.155 s
Ran all test suites matching /test\/foo.test.js/i.
SimenB commented 1 year ago

Duplicate of #438, fwiw