Jasmine fake timers and Node.js DNS lookup callback timing
0 reputation · 08 Nov 2020, 16:18 UTC
Jasmine fake timers and Node.js DNS lookup callback timing
The goal is to write a unit test that exercises DNS‑resolution logic while using Jasmine’s fake‑timer utility to make all asynchronous callbacks fire synchronously, enabling deterministic, fast test execution.
However, Jasmine’s jasmine.clock() only replaces JavaScript‑based timers (setTimeout, setInterval, etc.). Node’s dns.lookup and tls.connect callbacks are performed via libuv’s thread pool, so they are not intercepted by the fake timer and still run on real OS threads, causing the test to wait for actual I/O or to produce false passes if the timer is advanced prematurely.
This raises an unresolved design question about whether fake timers should also mock libuv‑based I/O to achieve fully synchronous test runs.
Should Jasmine extend its fake‑timer implementation to intercept libuv thread‑pool callbacks? What would be the impact on test fidelity and existing suites that rely on real DNS/TLS behavior? How can developers currently obtain synchronous‑style tests without modifying Jasmine core?