Jest issue. FakeTimers: clearTimeout was invoked to clear a native timer instead of one created by this library
Asked Answered
B

2

13

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.

Burial answered 19/1, 2022 at 9:54 Comment(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. github.com/sinonjs/… See: github.com/facebook/jest/blob/…Guardi
W
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/

Winnifredwinning answered 2/3, 2022 at 9:11 Comment(1)
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!Upstate
N
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.

Noisette answered 10/7, 2022 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.