Node's Built-in Test Runner
You'll learn how to write and run tests using Node.js's native test runner without installing any external dependencies.
What Is Node's Built-in Test Runner?
Starting with Node 18, JavaScript developers can write tests using a built-in node:test module and run them with the node --test command. This eliminates the need for third-party test runners when you just need simple, straightforward testing. Think of it as Node providing its own lightweight testing toolkit right out of the box—no npm install required.
Basic Usage
The test runner uses a simple API. Import the test function from node:test and write tests:
import { test } from 'node:test';
import assert from 'node:assert';
test('addition works correctly', () => {
assert.strictEqual(2 + 2, 4);
});
test('strings can be concatenated', () => {
const result = 'hello' + ' ' + 'world';
assert.strictEqual(result, 'hello world');
});
Run these tests with:
node --test
Node automatically discovers and runs all test files (typically those ending in .test.js or .spec.js).
Why Use the Built-in Runner?
For simple projects or learning, the native runner removes dependency overhead. You don't need to configure Jest, Mocha, or other tools—just write tests and run them. It's ideal when you want minimal setup but still need structured testing.
The built-in runner supports async tests, subtests, and basic reporting. While it lacks advanced features like mocking libraries or custom matchers, it covers fundamental testing needs perfectly well.
Key Takeaway: Node 18+ includes a native test runner (node:test) that lets you write and execute tests without installing external packages, perfect for straightforward testing scenarios.