> ## Content Index
> Fetch the complete content index at: https://logbuch-dev-blog.nichedthemes.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Testing it with node:test
- URL: https://logbuch-dev-blog.nichedthemes.com/testing-it-with-node-test/
- Published: 2026-11-02T07:30:00.000Z
- Updated: 2026-11-02T07:30:00.000Z
- Description: Node ships a test runner. Twelve tests cover the router and the middleware, and none of them opens a port.
- Author: Mira Okafor
- Tags: Backend, TypeScript, A tiny HTTP server in TypeScript, #Import 2026-11-19 07:37

Lene reviewed chapter 4 and asked where the tests were. Fair question. This chapter adds them, with the runner Node has shipped since version 18, and none of them starts the server.

Because handlers take a `Request` and return a `Response`, a test builds a request, calls `match`, and reads the response. No port, no client, no waiting.

## Run the tests

```console
$ pnpm test
```

```console
$ npm test
```

```json
{
  "type": "module",
  "scripts": {
    "test": "node --test test/"
  }
}
```

## A test for the router

```ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { match, route } from '../src/router.ts';

const get = (path: string) => new Request(`http://localhost${path}`);

route('GET', '/posts/:slug', (req, { slug }) => new Response(`You asked for: ${slug}`));

test('hands the slug to the handler', async () => {
  const res = await match(get('/posts/hello'));
  assert.equal(await res.text(), 'You asked for: hello');
});

test('answers 404 for an unknown path', async () => {
  const res = await match(get('/nope'));
  assert.equal(res.status, 404);
});
```

## What we did not test

`node:http` itself, and the four lines that turn its request into a `Request`. Node tests the first; the second is covered by one curl in chapter 6\. Twelve tests run in 41 ms on my laptop.

The sound my terminal plays when the suite passes

0:00

/2

1×