12

After the update of Node.js from version 14 to version 16, we've got a lot of failed tests on bitbucket CI/CD pipelines. Locally tests pass.

Seems like the problem in timers, cause the first error message says: "FakeTimers: clearTimeout was invoked to clear a native timer instead of one created by this library. To automatically clean-up native timers, use shouldClearNativeTimers". After that, a bunch of test fails.

After adding "--runInBand" parameter for jest it solves the issue, but it's not an ideal approach.

Will appreciate any help.

Alex Nepsha
  • 499
  • 5
  • 9
  • 1
    This warning comes from the sinonjs/fake-timers package. There does't appear to be a way to provide configuration directly to the fake timer in Jest. I was able to supress this warning by modifying the config passed to sinon in my node_modules. https://github.com/sinonjs/fake-timers#var-clock--faketimersinstallconfig See: https://github.com/facebook/jest/blob/66408c35dc22b4259d3e27c504921198ff143115/packages/jest-fake-timers/src/modernFakeTimers.ts#L102-L106 – theUtherSide Apr 05 '22 at 01:13

2 Answers2

6

i had this same issue.

Adding

jest.runOnlyPendingTimers()
jest.useRealTimers()

in the end of my test that had the useFakeTimers() fixed it.

reference: https://testing-library.com/docs/using-fake-timers/

dagadbm
  • 91
  • 5
  • I had an open handle in `Jest` that started happening after I introduced `jest.useFakeTimers().setSystemTime(new Date("this is not a date: is just a random date in the future"))` and putting this at the end of the test suite solved the open handle! thanks! – Henrique Aron Jan 16 '23 at 23:12
0

The answer from @dagadm did not work for me. It turns out I was using jest 26 which was the problem. The following fixed it for me.

npm i --save-dev jest@27 ts-jest@27 @types/jest@27

Then, of course, I ran into the "Test functions cannot both take a 'done' callback" issue which is solved here. I had to remove all my done functions from async tests.

Christopher Gertz
  • 1,826
  • 1
  • 15
  • 9