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
ResponseAPI:response.status,response.headers.get(),await response.json()orawait response.text(). - A request body, method and headers are set with the
Requestoptions: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.ipis unset andgetRequestIP(event)returnsundefined. To test code that reads it, type the request asAppRequestand assign an address torequest.ipbefore callingapp.fetch().- Handlers wrapped with
fromNodeHandler()orfromNodeMiddleware()depend on the Node.js adapter: see Express Compatibility for how to test an app that uses them.
Related
- Error Handling: the fallback response a test sees when no handler answers
- Plugin Conventions: testing a plugin the same way
- App reference:
app.fetch()and the App's options