mediumConceptDesignMidSeniorStaff
How do you test a Node service, and what do you refuse to mock?
6 min readUpdated 2026-08-15
Answer
Short answer
Drive business logic through injected dependencies rather than module mocks, intercept at the process boundary for outbound HTTP and time, and use a real database in a container for anything where the database is the logic — SQL, constraints, transactions and migrations.
Was this answer helpful?
Likely follow-ups
- Your suite is green but a release broke on a unique-constraint violation. Where was the gap, and what test would have caught it?
- How do you keep a Postgres-backed suite fast enough to run on every commit?
- When is a contract test the right answer instead of either a mock or a live dependency?
- How would you test code that depends on the current time and on a 30-second timeout?
Related questions
- A test passes on its own and fails when the whole suite runs. How do you track that down?mediumAlso on testing and jest5 min
- Your consumer-driven contract test passes in CI, but production rejects a request because a supposedly optional field is missing. What did the contract testing actually miss?hardAlso on integration-testing4 min
- How do you test a service that calls a third-party API, without calling it?mediumAlso on testing4 min
- What is the test pyramid, and is it still the right model for your system?mediumAlso on integration-testing4 min