Test asynchronous functionality in Jasmine 2.0.0 with done
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Asynchronous behavior is where many test suites become flaky or misleading. In Jasmine 2.0.0, the done callback is the main way to tell the runner, "this spec is not finished yet, keep waiting until I say it is."
Why done Exists
A normal Jasmine spec finishes as soon as the function body returns. That is fine for synchronous code, but it breaks when the code under test uses timers, callbacks, or network-style APIs. Without extra coordination, Jasmine would evaluate expectations too early or mark the test complete before the asynchronous work even runs.
Passing done into the it callback changes that contract. Jasmine waits until done() is called before deciding that the spec passed.
Basic Callback Example
The example below tests a function that calls its callback after a short delay.
The important part is the order of operations. The expectation runs inside the callback, and done() is called only after the assertion succeeds. If you called done() before the callback executed, Jasmine would finish the test too soon.
Testing Error Paths with done.fail
When asynchronous code throws or rejects later, a plain try block around the whole spec is not enough. The error happens on another turn of the event loop. Jasmine provides done.fail so you can fail the spec explicitly when the async path reports a problem.
This pattern is more reliable than letting an error disappear into the console while the test times out.
Coordinating Spies and Timers
A common use case is verifying that an async dependency was called with the expected data. Spies work well here, as long as you keep the expectation inside the async completion path.
You can also use Jasmine clock helpers in some cases, but done remains the simplest mental model when real callbacks are involved.
Timeout Behavior and Test Design
If done() is never called, Jasmine treats the spec as hung and eventually fails it on timeout. That is helpful because it exposes missing callbacks, unresolved promises, or branches that did not execute. Still, a timeout failure is usually a symptom, not a diagnosis. Keep asynchronous specs narrow so you can quickly identify whether the problem is timing, setup, or incorrect expectations.
In newer Jasmine codebases you may also see promise-based or async style tests. When maintaining Jasmine 2.0.0 specifically, done is the mechanism you should expect to use most often.
Common Pitfalls
- Forgetting to accept the
doneparameter causes Jasmine to treat the spec as synchronous. - Calling
done()before the assertion runs can create false positives. - Not handling the error path leads to timeouts instead of meaningful failures.
- Triggering multiple async branches and calling
done()more than once can produce confusing behavior. - Writing broad integration tests with many timers makes asynchronous failures harder to debug.
Summary
- '
donetells Jasmine to wait for asynchronous work before finishing the spec.' - Place expectations inside the callback or completion handler that proves the work finished.
- Use
done.failfor async error paths so failures are immediate and explicit. - Timeouts usually mean a branch never completed or
done()was never reached. - Keep async specs focused to make failures easier to diagnose.
Related reading
- Testing async function with jasmine
- Testing Complex Asynchronous Redux Actions
- tf.data Parallelize loading step
- TFRecordReader seems extremely slow , and multi-threads reading not working
- Test if an element is present using Selenium WebDriver
- Testing a Promise using setTimeout with Jest
- Test Container test cases are failing due to Could not find a valid Docker environment
- Test iOS app on device without apple developer program or jailbreak
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.