🏠 Home

NodeJs Test Runner

NodeJs has and has had an in-built test runner since version 18. It is damn nice.

At the time of writing version 22 has been released and will be LTS in October. That is to say besides the poor(in wins, not money) enterprise folks who have to beg for infrastructure updates we can make use of the in-built TestRunner and say goodbye to Jest forever!

Getting Started

Assuming you're using NodeJs version 20 you can create a simple test file like so:

import { describe, it } from "node:test";
import * as assert from "node:assert/strict";

const HUGE = "string";

describe("test suite", () => {
  it("test", () => {
    assert.ok(typeof "yourmom" === HUGE);
  });
});

and name it first.test.js in your project directory. Of course, this is Node so we have to have a package.json file so here is one, you can have it for free:

{
  "name": "tmp",
  "version": "1.0.0",
  "description": "",
  "main": "first.test.js",
  "type": "module",
  "scripts": {
    "test": "node --test"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The test script is setup to call the in-built test runner for node. Let's try using it.

terminal output showing the test suceeded in 47 milliseconds

47 milliseconds is fast for what we're used to in Node-land. Jest won't even run the test file without further configuration.

But what if this was a TypeScript file? That will require running the command:

npm install -D tsx

With that done we can update our package.json test script:

{
  "name": "tmp",
  "version": "1.0.0",
  "description": "",
  "main": "first.test.js",
  "type": "module",
  "scripts": {
    "test": "node --import tsx/esm --test $(find . -type f -name \"*.test.ts\")"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tsx": "^4.10.0"
  }
}

Run that new test script and you should see: terminal output showing the test suceeded in ~100 milliseconds

We definitely lost some speed but it is still faster than any Jest test I've seen. This is the way.

Finally, you may have noticed that I had to use the find command to collect the list of files to run tests on. This won't be required on versions 21 and greater, so if you can use those versions I'd highly recommend it.

Happy testing!