Node Test Runner vs Bun Test Runner (with TypeScript and ESM)
Tags: testing, node, bunjs, typescript
Published: 2024-04-26 17:01:51

I've recently gotten the chance to play with both of these while looking for a long-term testing framework at my job.
I also considered Node-TAP, Vitest, Jest and Mocha, but none truly convinced me as much as these two for a multitude of reasons (annoying config, lack of ESM support, speed, etc), so I'll focus on them.

# [Node Test Runner](https://nodejs.org/api/test.html)

## The good

Built-in. That's it, so simple, so nice. Plus it works with ESM too! 

## Use

It has everything you'd expect (`describe/it`, `mock`, `test`, `beforeEach`, etc...) and uses `assert` instead of the usual `expect`.

```ts
assert.equal(id.value, expectedId);
```

## The bad
No TS support out-of-the-box. So much so, that you need to do a hacky workaround in your `package.json` scripts:
```json
"test" "glob -c \"tsx --test --test-reporter spec \" \"./test/**/*.test.ts\""
```

You need both a loader (`tsx` or similar) and a way to tell it where your tests are (in this case, `glob`).

> This snippet comes from [the guide I made on setting up Node Test Runner for TypeScript](https://dev.to/boscodomingo/the-ultimate-node-test-runner-typescript-env-variables-guide-1hlj), so check it out if you want more info!

# [Bun Test Runner](https://bun.sh/docs/cli/test)

## The good

It's fast. Like twice as fast as Node's runner, which was already faster than Jest and Vitest. It also is as easy to set up as Node's Test Runner, and includes TS support as well!

## Use

It has a decent compatibility with both Jest and Vitest's APIs (you can track progress [here](https://github.com/oven-sh/bun/issues/1825) so you can use it as almost a drop-in replacement for either. 
Just as Node's, it has `describe/it`, `mock`, `test` and others, but with the `expect` syntax (which I find more readable). For example:

```ts
expect(id.value).toEqual(expectedId);
```

## The bad
Iffy debugging support. It requires the VS Code extension if you use VS Code, and I'm not sure how well other IDEs are supported. It also requires installing Bun as a dependency in `package.json` if you still use npm/pnpm/yarn to pin the right version.

# Verdict

If you already use Bun, or don't mind having to use additional tools for debugging, then Bun is definitely the choice to make. It's easier to use and faster, and has about the same functionality if not more than Node's.

If on the other hand you care about "native" experience, want the best CI installation times, or just want to use all-Node, then the good news is that Node's Test Runner is not far behind, and you won't feel like you're being left out for not using the most _blazingly fast_™ tool.

In any case, either one should be a good choice as they are seeing active development ([Node just released version 22](https://nodejs.org/en/blog/announcements/v22-release-announce), and [Bun announced v1.1 three weeks ago](https://bun.sh/blog/bun-v1.1) as of the time of writing) and aim to be easy-to-use, fast tools.

---

Hope this article was of use in helping you decide which one to go for, and don't forget to check all my other stuff for more posts like this one!