DEV Community

Craig Morten
Craig Morten

Posted on • Updated on

Testing Your Deno Oak Server Applications

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@v6.0.1/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@2.1.0/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!"}');
});
Enter fullscreen mode Exit fullscreen mode

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@2.1.1/mod.ts";
import { superoak } from "https://deno.land/x/superoak@2.1.0/mod.ts";
Enter fullscreen mode Exit fullscreen mode

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! πŸ¦•

Discussion (2)

Collapse
mark_tyers_462cb6380feba1 profile image
Mark Tyers

The sample code you provide throws 41 errors!

deno 1.18.2 (release, x86_64-apple-darwin)
v8 9.8.177.6
typescript 4.5.2
Enter fullscreen mode Exit fullscreen mode

There were so many errors that the dev discussion board refused to post them since they exceeded the maximum post length.

Cheers.

Collapse
craigmorten profile image
Craig Morten Author

Hey Mark!

This post is over a year old and unfortunately with the several changes to Deno in the time since, the out of date versions listed in this tutorial are no longer compatible (there have been a number of breaking changes in Deno for bug fixes).

Recommend using the examples in the repo github.com/cmorten/superoak and always upgrading to the latest version of Oak and SuperOak for the best results πŸ˜„