MyInternships.in

Testing, Performance & Deployment

Node.js Testing with the Built-in Test Runner

Node ships its own test runner from version 18, so unit tests need no external framework. It supports describe and it blocks, an assertion library, mocking and watch mode.


What is Testing with the Built-in Test Runner in Node.js?

Node ships its own test runner from version 18, so unit tests need no external framework. It supports describe and it blocks, an assertion library, mocking and watch mode.

Testing with the Built-in Test Runner example

A test file
JavaScript
import { test, describe, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { calculateStipend } from '../src/services/stipend.js';

describe('calculateStipend', () => {
  test('applies the city multiplier', () => {
    assert.equal(calculateStipend(10000, 'Bengaluru'), 12000);
  });

  test('rejects a negative base', () => {
    assert.throws(() => calculateStipend(-1, 'Pune'), /must be positive/);
  });

  test('resolves async work', async () => {
    await assert.doesNotReject(fetchRates());
  });
});

Running tests

Terminal
node --test                       # run every *.test.js
node --test --watch               # rerun on save
node --test --experimental-test-coverage

Key points to remember

  • assert/strict uses strict equality — prefer it over the loose version.
  • Vitest and Jest remain popular for their ecosystem and richer mocking.
  • Test services and pure functions first; they give the most value per line.

Common mistakes with Testing with the Built-in Test Runner

  • Tests that hit a real database or network, making them slow and flaky.
  • Asserting on implementation details rather than behaviour.

Node.js Testing with the Built-in Test Runner— Interview Questions & FAQs

Do I still need Jest for Node testing?+

Not for straightforward unit tests — the built-in runner covers them with zero configuration. Jest or Vitest are still worth it for extensive mocking, snapshot testing and a larger plugin ecosystem.

Related Node.js Topics

Keep learning with these closely related lessons.

Ready to use your Node.js skills?

Find verified Node.js internships and fresher developer jobs across India.

Browse Node.js Internships