QA
Intermediate LevelNode JS

How do you write unit and integration tests using the native node:test module and node:assert?

❓ Interview Prompt: import test from "node:test"; import assert from "node:assert"; test("math addition", async (t) => { assert.strictEqual(2 + 2, 4); });. Run via node --test. Zero-dependency test runner with mock support in Node 20+.

Node 20+ includes a built-in zero-dependency test runner ('node:test'):

Features:

•
Executes tests via 'node --test'.
•
Supports subtests, concurrent execution, mocking ('t.mock'), and code coverage ('--experimental-test-coverage').

Code Implementation Example

javascript
import test from 'node:test';
import assert from 'node:assert/strict';

test('Calculator addition', async (t) => {
  await t.test('adds positive numbers', () => {
    assert.strictEqual(2 + 3, 5);
  });
});
💡Writing zero-dependency unit tests using node:test and node:assert.
🎯 Interview Key Takeaway

Use native node:test in Node 20+ for zero-dependency unit testing and mocking.

#node:test#Testing#Assertions#Zero-Dependency#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?