Some context: you might want to test your node.js application in an integration test and your code might have a setInterval
call to repeatedly perform an action.
In this case I found that my tests would hang and timeout after some time.
The solution in my case was to include the following line in my test suite (note: Iām using node:test
built-in testing library):
import { test, describe, mock } from 'node:test'
mock.timers.enable({ apis: ['setInterval'] })
This will ensure that your setInterval
calls are properly mocked during the test, preventing them from interfering with your tests.
You could alternatively, as described in the docs, just use mock.timers.enable()
without any arguments.