http4js

Table of Contents

In Memory Testing

If we don’t start the server then we can still use it to serve requests in memory:

const routing = get("/path", async (req: Request) => ResOf(200))
    //.asServer()
    //.start()    
    
routing.serve(ReqOf("GET", "/path"))
     
// Response { headers: {}, body: '' , status: 200 }

This allows us to write unit tests that cover routing logic. Our test might look something like this:

describe("unknown routes", () => {

    it("404 page if no routes match", async () => {
        const request = new Req("GET", "/unknown-route");
        const testApp = new TestApp();

        const response = await testApp.serve(request);

        equal(response.status, 404);
        equal(response.bodyString(), "Page not found");
    });

});

where our test app is just our app with fake dependencies passed in

export class TestApp {
    routes: Routing;

    constructor(){
        const fakeDb = new FakeDb();
        const fakeSerice = new FakeService(fakeDb);
        this.routes = new App(fakeSerice).routes();
    }

    async serve(req: Req): Promise<Res> {
        return this.routes.serve(req);
    }
   
}

Prev: Routing API

Next: End to End Testing