Keploy logo
Fastify logo

Keploy as a Fastify testing framework

Keploy tests Fastify applications by recording real requests through the running server and every database or outbound call they trigger, then replaying both as assertions. Schema validation, hooks, and plugin decorators all execute because the request arrives over a real socket.

Generate Fastify tests free
keploy record -c "node dist/server.js"
18.4K+VS Code1.2M+300M+mocks created

What Keploy gives a Fastify team

Any Fastify app, unchanged

Keploy runs your real entrypoint. Plugins, encapsulation contexts, hooks, and JSON schema validation all execute because capture happens at the socket rather than inside the router.

  • Fastify 4 and above
  • Plugins and encapsulation preserved
  • JSON schema validation runs
  • TypeScript needs no extra setup
The problem

Why Fastify integration tests slow teams down

The friction is rarely the assertions. It is everything around them — spinning up dependencies, keeping mocks honest, and repairing tests after every refactor.

Three ways to do it

Testing Fastify: by hand, with tap + app.inject, or with Keploy

app.inject dispatches into an instance the test file assembles, so plugins from your entrypoint are missing. Keploy records the running server, so the app under test is the app you deploy.

Select any row for the full comparison, with code.

Same coverage, three costs

What you write for Fastify vs what Keploy records

All three produce the same assertion. Only the third still passes after the next refactor without anyone editing it.

By hand2–4 hours
orders.route.test.js
hand-written
// Hand-written: an instance built here, not in server.js.
const Fastify = require('fastify');
const ordersRoutes = require('../src/routes/orders');
 
test('confirms an authorized order', async (t) => {
const app = Fastify();
// Note what is missing: auth, rate limiting, and the error handler.
app.decorate('repo', { byId: async () => ({ id: 'ord_1', amountMinor: 4200 }) });
app.decorate('payments', { authorize: async () => ({ status: 'AUTHORIZED' }) });
await app.register(ordersRoutes);
 
const res = await app.inject({
method: 'POST',
url: '/orders/ord_1/confirm',
payload: { paymentToken: 'tok_123' },
});
 
t.equal(res.statusCode, 200);
});

The instance is assembled here, so @fastify/jwt and @fastify/rate-limit from server.js are absent and an unauthorised route still passes.

tap + app.inject1–2 hours
orders.inject.test.js
tool-assisted
// tap + app.inject + nock.
const nock = require('nock');
const buildApp = require('../src/app');
 
test('confirms an authorized order', async (t) => {
// A literal that will not change when Stripe does.
nock('https://api.payments.example.com')
.post('/v1/payment_intents/tok_123/confirm')
.reply(200, { status: 'AUTHORIZED' });
 
const app = await buildApp({ logger: false });
t.teardown(() => app.close());
 
const res = await app.inject({
method: 'POST',
url: '/orders/ord_1/confirm',
headers: { authorization: `Bearer ${token}` },
payload: { paymentToken: 'tok_123' },
});
 
t.equal(res.statusCode, 200);
t.equal(res.json().state, 'CONFIRMED');
});

Better — it uses the app factory — and the Stripe body is still a hand-typed literal, and a plugin registered outside buildApp is still missing.

With Keploy~5 minutes
test-1.yaml
auto-generated
# Recorded with: keploy record -c 'node dist/server.js'
# The real server. Every plugin and hook ran.
version: api.keploy.io/v1beta1
kind: Http
name: test-1
spec:
req:
method: POST
url: /orders/ord_1/confirm
header:
Authorization: Bearer <captured>
body: '{"paymentToken":"tok_123"}'
resp:
status_code: 200
header:
X-RateLimit-Remaining: "99"
body:
id: "ord_1"
state: "CONFIRMED"
amountMinor: 4200
noise:
- header.X-RateLimit-Remaining
mocks:
- kind: Postgres
operation: "SELECT id, amount_minor, state FROM orders WHERE id = $1"
- kind: Http
url: https://api.payments.example.com/v1/payment_intents/tok_123/confirm

The rate-limit header proves the real plugin chain ran, and the response passed through fast-json-stringify serialisation on the way out.

Times are estimates for authoring one endpoint’s coverage from scratch, not measurements.

Keploy vs the alternatives

Fastify testing tools, compared

The options a team on Node.js actually reaches for, and where each one genuinely wins. Select a row for the full comparison.

Best in classStrongPartialNot covered

Assessments reflect each tool’s documented behaviour, not benchmark measurements.

How it works

Record your Fastify app once, replay it forever

Keploy sits below your Fastify process at the network layer. It watches the calls your app already makes, then serves them back on replay so tests run with no dependencies attached.

Keploy records a GET call to /api/v1/orders/{id} on a Fastify service and captures the dependency calls it makes.

An example shape of a captured call. Your own endpoints and dependencies come from your real traffic, so nothing here has to be written by hand.
Mock coverage

What Keploy mocks for Fastify, with zero config

6 of the 6 dependencies a typical Fastify service talks to are stubbed from the recording itself — no mock classes, no fixture files, no containers in CI.

Quick start

Your first Fastify test suite in under five minutes

Every command below runs against your existing Fastify service. Nothing in your source tree changes.

  1. 1Install the Keploy CLI

    A single binary. It needs a Linux kernel with eBPF support, or Docker on macOS and Windows — and it adds nothing to your project's dependencies.

    curl -sSL https://keploy.io/install.sh | bash
  2. 2Record your service

    Pass the command you already use to start the app. Keploy runs it and watches every socket it opens.

    keploy record -c "node dist/server.js"
  3. 3Exercise the paths you care about

    Build as normal, then drive the running server. Every request becomes a test case with its database and HTTP calls captured alongside.

    npm run build
    curl -X POST localhost:3000/orders/ord_1/confirm -H 'Content-Type: application/json' -H 'Authorization: Bearer $TOKEN' -d '{"paymentToken":"tok_123"}'
  4. 4Replay in CI

    Replay serves the recorded dependency responses, so the job needs no service containers and no Docker daemon.

    keploy test -c "node dist/server.js" --delay 8

Ready to try it on your own Fastify service?

Ecosystem

Works with the rest of your Fastify stack

Keploy records at the network layer, so framework and driver choices inside your Fastify app do not change how it captures traffic.

FAQ

Fastify testing with Keploy: common questions

Join our GlobalCommunity

Connect with developers worldwide. Follow updates, ask questions, share feedback, and ship faster with other Keploy builders.

1.2M+Installs
18.4K+GitHub
100K+Devs
300M+Mocks
1K+Contributors
#1OSS Trending
4.9★★★★★from 500+ reviews onG2GartnerVS CodeChrome
★★★★★

Best report of integration and API tests I've seen — which we don't get from RestAssured.

G2
★★★★★

Future of microservices testing. I don't write tests now!

G2 · 5/5
★★★★★

An amazing product that simplifies the automation.

Gartner · 4.0
XGitHubSlackYouTubeLinkedIn
Built by developers, for developers.Let's build the future, together.