Node-TAP Test Framework

21.7.1 · active · verified Sun Apr 19

TAP (Test-Anything-Protocol) is a robust and opinionated test framework for Node.js, currently in version 21.7.1. It provides a comprehensive command-line test runner and a JavaScript/TypeScript framework for writing tests that output in the TAP format. A key differentiator is its philosophy of treating test files as "normal" programs, running each test in its own process to prevent state leakage and inter-test dependencies. Since version 18, `tap` has been entirely rewritten in TypeScript, offering first-class support for ESM, CommonJS, and TypeScript out-of-the-box, including rich, machine-generated type definitions for an enhanced developer experience with editor auto-completion. It features built-in test coverage (powered by `c8`), various reporter formats, and an extensive API leveraging a plugin-based architecture for core functionalities like assertions and mocking. The project maintains an active release cadence with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic synchronous and asynchronous tests, including assertions, type usage for the 't' object, and how to skip tests.

import tap from 'tap';
import type { Test } from 'tap';

interface MyApiResult {
  data: string;
  status: number;
}

async function fetchData(): Promise<MyApiResult> {
  // Simulate an API call
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({ data: 'Hello TAP!', status: 200 });
    }, 50);
  });
}

tap.test('Basic assertions and async test', async (t: Test) => {
  t.plan(3); // Declare the number of assertions expected

  t.ok(true, 'should be truthy');
  t.equal(1 + 1, 2, 'addition works');

  const result = await fetchData();
  t.deepEqual(result, { data: 'Hello TAP!', status: 200 }, 'async data matches expected structure');
});

tap.test('Skipping a test', (t: Test) => {
  t.skip('This test is intentionally skipped for now');
  t.end(); // Must call t.end() for non-async tests unless t.plan() is used.
});

tap.test('Synchronous operations', (t: Test) => {
  const arr = [1, 2, 3];
  t.notHas(arr, 4, 'array should not contain 4');
  t.throws(() => {
    throw new Error('Expected error');
  }, 'Expected error was thrown');
  t.end();
});

view raw JSON →