loading...

Testing Your Deno Oak Server Applications

craigmorten profile image Craig Morten ・2 min read

Excited to share that I've been working on a couple of testing libraries for Deno HTTP servers that are now ready to use!

  • SuperDeno - HTTP assertions for Deno made easy via superagent.
  • SuperOak - an extension to SuperDeno to provide easy HTTP assertions for Deno's popular Oak web framework.

Here's an example of how you can use SuperOak to test an Oak server:

// server.ts
import { Application, Router } from "https://deno.land/x/oak@master/mod.ts";

const router = new Router();
router.get("/", (ctx) => {
  ctx.response.body = { message: "Hello Deno!" };
  ctx.response.status = 200;
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

export default app;


// server.test.ts
import { superoak } from "https://deno.land/x/superoak@master/mod.ts";
import app from "./server.ts";

/**
 * Test that the server returns the "Hello Deno!" JSON object when make a
 * GET request to "/".
 */
Deno.test("it should return some JSON with status code 200", async () => {
  const request = await superoak(app);
  await request.get("/")
    .expect(200)
    .expect("Content-Type", /json/)
    .expect('{"message":"Hello Deno!"}');
});

About

Currently there is quite a lot of bootstrapping and setup required to test your Opine, Oak and other web framework applications.

These modules aim to provide an easy to use, high-level abstraction for testing HTTP in Deno, while still allowing you to drop down to a lower-level API provided by superagent.

Installation

This is a Deno module available to import direct from this repo and via the Deno Registry.

Before importing, download and install Deno.

You can then import SuperDeno or SuperOak straight into your project:

import { superdeno } from "https://deno.land/x/superdeno@master/mod.ts";
import { superoak } from "https://deno.land/x/superoak@master/mod.ts";

If you want to use a specific version of SuperDeno or SuperOak, just modify the import url to contain the version:

import { superdeno } from "https://deno.land/x/superoak@1.1.2/mod.ts";
import { superoak } from "https://deno.land/x/superoak@1.0.1/mod.ts";

Or if you want to use a specific commit of SuperDeno or SuperOak, just modify the import url to contain the commit hash:

import { superdeno } from "https://deno.land/x/superoak@c21f8d6/mod.ts";
import { superoak } from "https://deno.land/x/superoak@c4eb4a8/mod.ts";

Further API docs and further examples are available in the GitHub READMEs (linked above)! 🚀

Let me know how you're getting on with writing and testing your HTTP Servers in Deno! Questions, suggestions and issues all welcome - drop a message in the comments below or on in the GitHub repository issues section.

Happy testing! 🦕

Posted on May 23 by:

craigmorten profile

Craig Morten

@craigmorten

26 • London • That JS Guy • JavaScript, TypeScript, React, Node, Deno, Kubernetes, Azure • I also tweet stuff

Discussion

markdown guide