Skip to content

arkstack-hq/parasito

Repository files navigation

Parasito

NPM Downloads npm version License codecov Test Publish to NPM

Universal TypeScript-first HTTP testing library for Node applications.

Parasito attaches one request API to framework apps, Node servers, and fetch-style handlers. Use it to test Express, Fastify, H3, Hono, Koa, plain Node handlers, already-listening servers, or remote URLs with the same fluent assertions.

Install

pnpm add -D parasito

Quick Start

import request from 'parasito';
import express from 'express';

const app = express();

app.get('/account', (_request, response) => {
  response.json({ ok: true });
});

await request(app).get('/account').expect(200).expect({ ok: true });

Supported Targets

  • Express and connect-style handlers
  • Fastify instances
  • H3 apps
  • Hono apps
  • Koa apps
  • Plain Node request handlers
  • Node http.Server instances
  • Fetch-style functions and objects with a fetch(request) method
  • Remote HTTP URLs

API

await request(app)
  .post('/account')
  .set('authorization', 'Bearer token')
  .query({ expand: 'team' })
  .send({ name: 'Ada' })
  .expect(201)
  .expect('content-type', /json/)
  .expect((response) => {
    // Use your test runner's assertions here.
  });

Requests are promise-like, so you can await them directly. The resolved response includes:

  • status and statusCode
  • ok
  • headers as Web Headers
  • header as a plain object
  • text
  • body
  • raw

Examples

H3

import { H3 } from 'h3';
import request from 'parasito';

const app = new H3();

app.get('/account', (event) => {
  return {
    authorization: event.req.headers.get('authorization'),
    ok: true,
  };
});

await request(app).get('/account').auth('token').expect(200).expect({
  authorization: 'Bearer token',
  ok: true,
});

Hono

import { Hono } from 'hono';
import request from 'parasito';

const app = new Hono();

app.get('/account', (context) => {
  return context.json({ ok: true });
});

await request(app).get('/account').expect(200).expect({ ok: true });

Fastify

import fastify from 'fastify';
import request from 'parasito';

const app = fastify();

app.post('/account', async (request) => {
  return { body: request.body };
});

try {
  await request(app)
    .post('/account')
    .send({ name: 'Ada' })
    .expect(200)
    .expect({ body: { name: 'Ada' } });
} finally {
  await app.close();
}

Koa

import Koa from 'koa';
import request from 'parasito';

const app = new Koa();

app.use((context) => {
  context.body = { ok: true };
});

await request(app).get('/account').expect(200).expect({ ok: true });

Plain Node

import request from 'parasito';

await request((_incoming, outgoing) => {
  outgoing.setHeader('content-type', 'application/json');
  outgoing.end(JSON.stringify({ ok: true }));
})
  .get('/health')
  .expect(200)
  .expect({ ok: true });

Remote URLs

Use a string URL when the service is already running outside the current test process.

import request from 'parasito';

await request('https://api.example.com')
  .get('/health')
  .expect(200)
  .expect({ ok: true });

URL objects are supported too.

import request from 'parasito';

const api = new URL('https://api.example.com');

await request(api)
  .post('/account')
  .auth('token')
  .send({ name: 'Ada' })
  .expect(201);

Development

pnpm install
pnpm lint
pnpm typecheck
pnpm test
pnpm build

About

Universal TypeScript-first HTTP testing library for Express, Fastify, H3, Hono, Node servers, and fetch-style apps.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors