# Node 18 shipped fetch and a test runner. I kept one.

> Two dependencies became built-ins in April, both marked experimental. One of them I removed from a project the same week; the other I am leaving exactly where it is, for reasons that have little to do with the feature.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-07-16
- Tags: Node.js, JavaScript, TypeScript, Testing
- URL: https://deyanpeev.com/posts/node-eighteen-fetch-and-a-test-runner

Node 18 arrived in April with two additions that each delete a dependency a lot
of projects carry, a global `fetch` and a test runner in core. Both are marked
experimental. Node 18 becomes the LTS line in October, and that's when most of
the projects I touch will meet it.

Three months in, I've adopted one of them and left the other alone. The
reasons have less to do with the features than with what a dependency is for.

# fetch, which I took

`fetch` is now a global, along with `FormData`, `Headers`, `Request` and
`Response`. No import, no package.

```js
const res = await fetch('https://api.example.com/things', {
  headers: { authorization: `Bearer ${token}` },
})

if (!res.ok) throw new Error(`things: ${res.status}`)
const body = await res.json()
```

That's the same code I'd write in a browser. A codebase that shares utilities
between a front end and a Node service has spent years either shipping a
polyfill or writing two versions of every HTTP helper.

`fetch` does not reject on a 404 or a 500. It resolves, and that catches people.
`res.ok` is the check, and a wrapper that forgets it turns every server error
into a JSON parse failure two layers away. Anyone coming over
from `axios`, which throws on non-2xx by default, will write this bug once.

It can be turned off with `--no-experimental-fetch`, and I read that as a hint
about how the Node team thinks about the stability label.

# The test runner, which I did not

`node:test` gives you a test runner with no install, producing TAP output:

```js
import test from 'node:test'
import assert from 'node:assert/strict'

test('splits keywords on whitespace', () => {
  assert.deepEqual(splitKeywords('  a  b '), ['a', 'b'])
})
```

It's a perfectly reasonable runner. For a small library with no dependencies
(the kind of package where adding a test framework doubles the install size) I
would use it today.

I'm not moving anything else though. What I use Jest for is mostly the stuff
around the `test()` function and the assertions: watch mode that reruns only
what changed, module mocking, coverage that is wired up without me thinking
about it, snapshot testing. And everyone who joins the project already knows how
it behaves.

A runner in core replaces only the smallest part of that. Switching an existing
suite over means giving up tooling I use daily, and all I'd get for it is one
dev dependency less, which costs me nothing at runtime anyway.

# The distinction I came away with

`fetch` is a **standard** arriving in a runtime. The API was specified elsewhere
and implemented everywhere, and what Node was missing was the implementation.
Adopting it moves my code towards a spec that outlives any library, and the
migration is deleting an import.

`node:test` is a **tool** arriving in a runtime. There's no specification for
what a test runner should be. The ecosystem's options are different because
their authors wanted them different, and most of the value of the one I use is
in the bits core doesn't have.

So my rule for built-ins is to take the ones that replace a polyfill and be
slower about the ones that replace a choice. A polyfill exists because the
runtime was behind, and once the runtime catches up there's no reason left to
keep it. A tool exists because somebody made decisions, and core shipping its
own set of decisions doesn't retire the others.

# On "experimental"

For `fetch`, experimental means the API surface might still move at the edges.
But that API surface is a web standard and a great many things depend on it, so
the risk of it changing under me in a way that matters is low. And the escape
hatch is a flag.

For the test runner it means something heavier, because a test suite is a large
investment in one tool's semantics. Rewriting a suite twice is a bad afternoon,
and doing it because a label changed would be self-inflicted.

I'll look at both again when 18 becomes LTS in October. By then "experimental in
Node" starts meaning something different anyway.
