fetch-test-server is a utility library for Node.js integration testing, enabling developers to test their HTTP servers (including frameworks like Express and Koa) using the modern Fetch API. As of version 1.2.0, it leverages native ES6 features and promises, providing an async/await-friendly alternative to callback-based testing libraries such as SuperTest. The library operates by starting the user's HTTP server on a random available port, internally using `node-fetch` to make requests. It intelligently defers server startup until the first request is made, streamlining the test setup process. Its release cadence appears moderate, with recent updates focusing on modern JavaScript features. This package differentiates itself by offering a familiar browser-like API for server interactions directly within Node.js test environments.
npm install fetch-test-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express server and testing its API endpoints using `fetch-test-server` with `async/await` syntax, showing GET and POST requests with JSON bodies and custom headers. It also includes server lifecycle management.
Migrate your test files and project configuration to use ES Modules. This typically involves changing `require()` to `import` statements and ensuring your `package.json` has `"type": "module"` or using `.mjs` file extensions for your test files.
Be aware that server resources are not consumed until the first request. If you need the server address or explicit control over startup, call `await server.listen()` before making requests or accessing `server.address`.
Instead of `new TestServer(app)`, use `new TestServer(app.callback())` for Koa applications.
Switch to ES Module `import` syntax: `import { TestServer } from 'fetch-test-server';`. Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions for ES Modules.For Koa apps, instantiate `TestServer` with `new TestServer(app.callback())`.