TypeScript Node.js Test Runner Wrapper

0.4.4 · active · verified Tue Apr 21

ts-node-test is a command-line interface (CLI) wrapper that enables the use of Node.js's native test runner (introduced in Node.js 18.7.0) with TypeScript files. It addresses a limitation in Node.js where the built-in test runner does not natively support custom file extensions for automatic test file discovery, requiring explicit file paths for `.ts` files. This package works by recursively searching specified directories for `.ts`, `.mts`, `.cts`, `.js`, `.mjs`, and `.cjs` files, then passing the discovered files to the Node.js test runner via `ts-node` for execution. The current stable version is 0.4.4, with releases occurring intermittently, focusing on bug fixes, dependency updates, and feature parity with newer Node.js test runner flags. Its primary differentiator is simplifying the setup for TypeScript projects wanting to leverage Node.js's native testing capabilities without complex build steps or manual file listings.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `ts-node-test` in a `package.json` script, along with a simple TypeScript test file using Node.js's native test runner API, including synchronous, asynchronous, mocked, and sub-tests.

/* package.json */
{
  "name": "my-ts-project",
  "version": "1.0.0",
  "scripts": {
    "test": "ts-node-test 'src/**/*.test.ts' 'test/**/*.test.ts'"
  },
  "devDependencies": {
    "ts-node-test": "^0.4.4",
    "typescript": "^5.0.0",
    "@types/node": "^18.7.0" 
  },
  "engines": {
    "node": ">=18.7.0"
  }
}

/* test/example.test.ts */
import { test, mock } from 'node:test';
import { equal, ok } from 'node:assert/strict';

const greet = mock.fn((name: string) => `Hello, ${name}!`);

test('synchronous passing test', () => {
  equal(1, 1, 'Numbers should be equal');
});

test('asynchronous passing test', async () => {
  const promise = Promise.resolve(42);
  const result = await promise;
  equal(result, 42, 'Result should be 42');
});

test('test with mocks and assertions', () => {
  greet('World');
  equal(greet.mock.callCount(), 1, 'greet should be called once');
  equal(greet.mock.calls[0].arguments[0], 'World', 'Argument should be World');
  equal(greet.mock.calls[0].result, 'Hello, World!', 'Return value should be correct');
});

test('test with subtest and tags', { tags: ['core', 'feature'] }, async (t) => {
  await t.test('subtest 1: addition', () => {
    ok(2 + 2 === 4, '2 + 2 should be 4');
  });
  await t.test('subtest 2: skipped example', { skip: true }, () => {
    // This test will be skipped and not executed
    equal(true, false, 'This assertion should not run if skipped');
  });
});

// To run: 
// 1. npm init -y
// 2. npm i -D ts-node-test typescript @types/node
// 3. Create package.json and test/example.test.ts as shown above
// 4. npm test

view raw JSON →