Mocha - running and resolving two async/await calls
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Mocha, the clean way to test two asynchronous operations is to make the test function async and then await the promises you care about. The real decision is whether those operations should run sequentially or in parallel, because the Mocha part is simple once you understand that distinction.
Use an async Test Function
Mocha understands a returned promise. An async test automatically returns one, so this is enough:
Mocha waits for the test promise to resolve. If one of the awaited calls rejects, the test fails automatically.
Run the Calls Sequentially When Order Matters
The previous example is sequential. getSettings() does not start until getProfile() has finished. That is correct when:
- the second call depends on the first result
- the order is part of the behavior being tested
- you want failure to stop the rest of the flow immediately
Here is a more explicit dependent example:
In this case, sequential awaits are not just acceptable, they are the right model.
Use Promise.all When the Calls Are Independent
If both async calls can happen at the same time, start them together and await them together:
This is often faster and makes the intent clear: the operations are independent, and the test should wait for both.
Handle Expected Failures Deliberately
When you want to assert that one of the async calls rejects, use try/catch or a promise-aware assertion library:
This is clearer than letting the rejection escape when the test is supposed to verify the failure itself.
Do Not Mix done with async
Mocha supports the older done callback style, but you should not combine it with async/await in the same test:
That pattern is error-prone because it mixes two completion models. If you are using async, let the returned promise control test completion.
Common Pitfalls
The biggest mistake is writing two await calls sequentially when the operations are actually independent and could be tested with Promise.all.
Another common issue is forgetting to await one of the promises. The test may finish early, producing false positives or unhandled rejection noise later.
People also mix done, returned promises, and async in one test. Pick one async style and keep it consistent.
Finally, do not swallow errors accidentally. If you catch an error in a test, make sure you still assert something about it or rethrow it.
Summary
- In Mocha, an
asynctest function is enough for promise-based testing. - Use sequential
awaitwhen one async call depends on another. - Use
Promise.allwhen two async calls are independent and should run in parallel. - Assert expected rejections explicitly with
try/catchor promise-aware assertions. - Do not mix
donecallbacks withasync/awaitin the same test.

