Testing Node.js in 2026: node:test, Vitest and Supertest
Node.js now ships with a capable built-in test runner, and the ecosystem has moved toward faster, ESM-friendly tools. Here's a modern testing setup.
Option 1: The Built-in node:test
No dependencies required:
// sum.test.js
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import { sum } from './sum.js';
describe('sum', () => {
test('adds numbers', () => {
assert.equal(sum(2, 3), 5);
});
});
node --test
node --test --watch
node --test --experimental-test-coverage
It supports mocking (mock.fn(), mock.method()), timers, snapshots, and coverage.
Option 2: Vitest
Vitest offers a Jest-compatible API, fast execution, great ESM and TypeScript support, and a watch mode many developers love.
import { describe, it, expect, vi } from 'vitest';
it('calls the mailer once', async () => {
const send = vi.fn().mockResolvedValue(true);
await notifyUser({ send }, 'a@b.com');
expect(send).toHaveBeenCalledTimes(1);
});
Jest remains common in existing projects, but new projects increasingly choose Vitest or node:test.
API Tests With Supertest
import request from 'supertest';
import { app } from '../app.js';
import { test } from 'node:test';
import assert from 'node:assert/strict';
test('GET /health returns ok', async () => {
const res = await request(app).get('/health');
assert.equal(res.status, 200);
assert.deepEqual(res.body, { status: 'ok' });
});
Export the app without calling listen() so tests can use it directly.
What to Test
- Unit tests — business logic, pure functions
- Integration tests — routes + database (use a test database or containers)
- Contract tests — external API shapes
- Don't chase 100% coverage; cover critical paths and bug fixes
In CI
- run: npm ci
- run: npm test
Fail the build on test failures, and run tests on every pull request.
AI Helps Here
AI coding assistants are excellent at writing test cases — especially edge cases you forgot. Review them: a test that passes for the wrong reason is worse than no test.
Fast tests get run. Slow tests get skipped. Optimise for a test suite developers actually run before every push.