Node.js Developer interview question
What is your Node.js testing strategy?
Test pure modules quickly, exercise HTTP handlers with real serialization, integrate with disposable databases where queries matter, verify async failures and cancellation, and keep only critical user paths end to end.
Direct answer
What a strong answer covers
How it works
Pure modules get fast unit tests with the built-in test runner or vitest and no I/O. HTTP handlers are tested through the real router with supertest so serialisation, validation, and status codes are exercised rather than mocked. Modules that build queries run against a disposable PostgreSQL in Docker because a mocked query proves nothing about SQL. Async behaviour is tested explicitly: timeouts, cancellation with AbortSignal, and rejection paths. A small set of end-to-end tests covers the flows that matter, and the whole suite is parallel-safe by giving each test its own data.
Example
The pricing module has forty table-driven unit tests. The orders router is tested with supertest against a test container database, asserting the 422 body for invalid input and the 201 Location header for success. One test asserts that aborting the request signal cancels the upstream fetch. The end-to-end job runs checkout in a browser nightly against staging.
Common mistakes
- Mocking the database layer and shipping a broken SQL query with green tests.
- Sharing a global database fixture between parallel tests and getting order-dependent failures.
- Testing async code with setTimeout waits instead of awaiting the actual promise.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.