Tests should await owned asynchronous work and isolate shared state. A test that finishes early can miss errors or affect later tests.
Before you start
You should know JavaScript promises, asynchronous errors and the distinction between a process and a request. When following a server example, identify the resource owner and the point where work completes. Try experiments locally with bounded input instead of assuming production traffic behaves like a single request.
The practical goal is to reason through this situation: Await an HTTP operation and close its server in cleanup. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Await owned operations
A test must remain active until the asynchronous behavior it asserts has completed or rejected.
Step 2: Control dependency completion
Use an explicit deferred adapter rather than guessing that a short delay is enough.
Step 3: Clean shared resources
Close servers and reset shared state in cleanup even when assertions fail. Prevent one test from affecting another.
Worked scenario
Await an HTTP operation and close its server in cleanup.
A rejection test waits for the operation and asserts that rejection rather than launching it and finishing immediately. A server started for the test is closed in finally or test cleanup. Without that ownership, late callbacks can mutate the next test’s state or leave an open handle keeping the suite alive.
Common mistake
Open handles and unawaited promises can produce flaky results.
Verify the behavior
Run tests in different orders and repeatedly. Check rejected operations are asserted and active handles return to baseline.
Interview exercise
Test a rejected operation.
Answer and reasoning
Assert the rejection explicitly and verify cleanup; avoid timing guesses when a controlled dependency can signal completion.
Continue learning
Compare the scenario with the Node.js interview questions and test your understanding with the Node.js MCQs. For terminology and implementation details, consult the reference material.