Use the runtime's supported test APIs
Import the test function and strict assertions using the module format the project already supports. Run the suite with the configured Node test command. Check the documentation for the installed runtime before adopting newer test features, because availability and stability can differ from examples written for another Node release.
Start with a pure function such as parsing a configuration value. Assert the exact object or error defined by the contract. Keep process startup and HTTP listening outside this small check unless they are part of the target behavior, since they add state and cleanup concerns that obscure an otherwise local failure.
Keep asynchronous and resource lifetimes explicit
Await the operation under test and any setup needed before it. A pending promise by itself is not proof that the expected work finished correctly. For callbacks or streams, use the supported test structure and explicit completion conditions so failures and cleanup occur within the lifetime observed by the runner.
Create temporary directories for file cases and remove or release resources through cleanup hooks. Close servers and connections after failures as well as successes. Avoid changing process-wide environment variables in concurrently running cases unless isolation is explicit, because another case can observe the modified value and produce a misleading result.
Separate local behavior from service integration
Inject a small lookup function when a rule needs remote data. Use controlled returns and errors to cover local decision paths. Keep a separate check for the real HTTP contract or database behavior so a passing fake does not imply that the application can communicate correctly with its deployed dependency today.
Inspect discovery and test output in continuous integration, including whether any cases were skipped or failed only after retries. A clean run should identify the tested revision and command. Use a representative deliberate defect to confirm that an important assertion actually detects the behavior it is meant to protect in later changes.
Working sheet
| Behavior | Setup | Assertion |
|---|---|---|
| Config parsing | Direct input | Exact parsed value |
| File transformation | Temporary directory | Expected file content |
| Remote error handling | Injected failing lookup | Defined error result |
| HTTP contract | Controlled server boundary | Status and payload |
Example to adapt
Illustrative example. Run it against your own fixtures and supported tool versions.
import test from 'node:test';
import assert from 'node:assert/strict';
test('empty input produces no tags', () => {
assert.deepEqual(parseTags(''), []);
});
// Import parseTags from the application module.NestJS unit testing
Build a focused testing module with the provider under test and replace its external collaborators with explicit test doubles. Assert an observable return value or interaction required by the contract.
Use a separate application-level test for routing, pipes and guards. Calling a controller method directly does not establish that HTTP validation or authentication runs correctly.
Questions about write Node.js unit tests with the built-in test runner
Is node:test available in every Node version?
Check the installed runtime's documentation and project support policy before using particular features.
Can the built-in runner test asynchronous code?
Yes. Use its supported asynchronous patterns and ensure the operation is awaited or otherwise tracked correctly.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
