Testing Asynchronous Code
Techniques and common pitfalls for correctly testing promise-based and callback-based asynchronous code, including fake timers.
Testing asynchronous code introduces a class of pitfalls that don't exist in synchronous tests, primarily because a test can finish and be marked 'passed' before an asynchronous operation it kicked off has actually completed, unless the test framework is explicitly told to wait for it — a subtle but common source of tests that appear to pass while silently never actually verifying anything.
Testing async code without awaiting it properly is like a teacher collecting exam papers and grading the class immediately, without actually waiting for every student to finish writing — a few unfinished papers might get graded as blank or fail silently, even though the student was still legitimately in the middle of writing a correct answer when the papers were prematurely collected.