Skip to content

Testing ​

An App is tested without a server: app.fetch() takes a Web Request and resolves with the Response a client would receive. Any test runner works, and routup ships no test helpers.

typescript
const response = await app.fetch(new Request('http://localhost/users/1'));
  • The request URL must be absolute. Routing reads its method and path, so any host works.
  • Status, headers and body are read with the standard Response API: response.status, response.headers.get(), await response.json() or await response.text().
  • A request body, method and headers are set with the Request options: new Request(url, { method: 'POST', headers, body }).

A Vitest Example ​

The file below defines a small app with a middleware, a route and an error handler, and tests each of them. It runs with npx vitest.

ts
import { describe, expect, it } from 'vitest';
import {
    App,
    AppError,
    defineCoreHandler,
    defineErrorHandler,
} from 'routup';

const users: Record<string, { name: string }> = { 1: { name: 'Alice' } };

const app = new App();

app.use(defineCoreHandler((event) => {
    event.response.headers.set('x-powered-by', 'routup');

    return event.next();
}));

app.get('/users/:id', defineCoreHandler((event) => {
    const user = users[event.params.id!];

    if (!user) {
        throw new AppError({ status: 404, message: 'User not found' });
    }

    return user;
}));

app.use(defineErrorHandler((error, event) => {
    event.response.status = error.status;

    return { error: error.message };
}));

describe('users app', () => {
    it('should answer a known user as JSON', async () => {
        const response = await app.fetch(new Request('http://localhost/users/1'));

        expect(response.status).toBe(200);
        expect(response.headers.get('content-type')).toMatch(/^application\/json/);
        expect(await response.json()).toEqual({ name: 'Alice' });
    });

    it('should set a header in the middleware', async () => {
        const response = await app.fetch(new Request('http://localhost/users/1'));

        expect(response.headers.get('x-powered-by')).toBe('routup');
    });

    it('should shape the error in the error handler', async () => {
        const response = await app.fetch(new Request('http://localhost/users/2'));

        expect(response.status).toBe(404);
        expect(response.headers.get('x-powered-by')).toBe('routup');
        expect(await response.json()).toEqual({ error: 'User not found' });
    });
});

The middleware sets its header on event.response, so the header is also on the response the error handler builds.

What a Plain Request Lacks ​

app.fetch() called directly skips the server adapter, so the request has none of the fields the adapter adds:

  • event.request.ip is unset and getRequestIP(event) returns undefined. To test code that reads it, type the request as AppRequest and assign an address to request.ip before calling app.fetch().
  • Handlers wrapped with fromNodeHandler() or fromNodeMiddleware() depend on the Node.js adapter: see Express Compatibility for how to test an app that uses them.