• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP assertions for Oak made easy via SuperDeno. 🐿 πŸ¦•

Super Oak standing in the rain at night – stoically facing the dark battle that is software engineering

SuperOak

HTTP assertions for Deno's Oak web framework made easy via SuperDeno.

Current version Current test status SuperOak docs PRs are welcome SuperOak issues SuperOak stars SuperOak forks SuperOak license SuperOak is maintained

SuperOak latest /x/ version Minimum supported Deno version SuperOak dependency count SuperOak dependency outdatedness SuperOak cached size


Table of Contents

About

This module aims to provide a high-level abstraction for testing HTTP in Deno's Oak web framework. This is a wrapper compatibility layer around SuperDeno to reduce some of the boilerplate needed to setup Oak integration + functional tests.

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 SuperOak straight into your project:

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

SuperOak is also available on nest.land, a package registry for Deno on the Blockchain.

Note: All examples in this README are using the unversioned form of the import URL. In production you should always use the versioned import form such as https://deno.land/x/[email protected]/mod.ts.

Example

You may pass a url string (for an already running Oak server), or an Oak Application object to superoak() - when passing an Oak Application, SuperOak will automatically handle the creation of a server, binding to a free ephemeral port and closing of the server on a call to .end().

SuperOak works with any Deno test framework. Here's an example with Deno's built-in test framework.

import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { superoak } from "https://deno.land/x/[email protected]/mod.ts";

const router = new Router();
router.get("/", (ctx) => {
  ctx.response.body = "Hello Deno!";
});
router.post("/user", (ctx) => {
  ctx.response.body = "Post!";
});

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

// Send simple GET request
Deno.test("it should support the Oak framework", async () => {
  const request = await superoak(app);
  await request.get("/").expect("Hello Deno!");
});

// Custom requests can be built with the superagent API
// https://visionmedia.github.io/superagent/#post--put-requests.
Deno.test("it should allow post requests", async () => {
  const request = await superoak(app);
  await request.post("/user")
    .set("Content-Type", "application/json")
    .send('{"name":"superoak"}')
    .expect(200);
});

Save the above to a file demo.test.ts and test it using deno test --allow-net demo.test.ts.

For further examples, see the SuperOak examples, tests or the SuperDeno examples for inspiration.

Documentation

API

Please refer to the SuperDeno API and SuperAgent API.

FAQs

Property 'get' does not exist on type 'Promise<SuperDeno>' error

Unlike SuperDeno, superoak() returns a promise which will need to be awaited before you can call any method such as .get("/").

// βœ… works
Deno.test("it will allow you to make assertions if you await it", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!");
});

// ❌ won't work
Deno.test("it will allow you to make assertions if you await it", async () => {
  const request = superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!"); // Boom πŸ’₯ `Property 'get' does not exist on type 'Promise<SuperDeno>'`
});

Request has been terminated error

Unlike SuperDeno, you cannot re-use SuperOak instances. If you try you will encounter an error similar to below:

Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
    at Test.Request.crossDomainError
    at XMLHttpRequestSham.xhr.onreadystatechange
    ...

This is because SuperOak instances automatically close the underlying Oak server once the assertion chain has completed.

Instead you should make all of your assertions on a single SuperOak instance, or create a new SuperOak instance for subsequent assertions like below:

// βœ… works
Deno.test("it will allow you to make multiple assertions on one SuperOak instance", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!");
});

// βœ… works
Deno.test("it will allow you to re-use the Application for another SuperOak instance", async () => {
  const request1 = await superoak(app);
  await request1.get("/").expect(200);

  const request2 = await superoak(app);
  await request2.get("/").expect("Hello Deno!");
});

// ❌ won't work
Deno.test("it will throw an error if try to re-use a SuperOak instance", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200);
  await request.get("/").expect("Hello Deno!"); // Boom πŸ’₯ `Error: Request has been terminated`
});

Contributing

Contributing guide


License

SuperOak is licensed under the MIT License.

Icon designed and created by Hannah Morten.

More Repositories

1

opine

Minimalist web framework for Deno ported from ExpressJS.
TypeScript
854
star
2

superdeno

Super-agent driven library for testing Deno HTTP servers.
TypeScript
122
star
3

deno-rollup

Next-generation ES module bundler ported for Deno
TypeScript
74
star
4

oak-http-proxy

Proxy middleware for Deno Oak HTTP servers. 🐿 πŸ¦•
TypeScript
41
star
5

luath

Fast front-end development tooling in Deno.
TypeScript
35
star
6

cypress-web-vitals

cypress-web-vitals
JavaScript
19
star
7

deno-react-base-server

Minimal React SSR Base Server in Deno.
TypeScript
17
star
8

opine-http-proxy

Proxy middleware for Deno Opine HTTP servers.
TypeScript
14
star
9

refresh

Simple browser reload on file change middleware for your Deno web applications.
TypeScript
14
star
10

importw

Permission restricted imports for Deno.
TypeScript
12
star
11

grafana-jsx

A JSX library for creating JSON for Grafana.
JavaScript
6
star
12

opine-cli

Opine's application generator
TypeScript
6
star
13

permission-guard

A zero-dependency, minimal permission guard for Deno.
TypeScript
4
star
14

serialize-server-state

A fast, secure serializer for server JSON state.
JavaScript
2
star
15

native_http

Native Deno HTTP client and server implementations
TypeScript
2
star
16

json-jsx

A JSX library for creating JSON.
JavaScript
2
star
17

startr

Yet another opinionated startup script for new macs.
Shell
2
star
18

ayup

For being lazy with what you test.
JavaScript
1
star
19

deno-rollup-react-example

An example of how you can use Rollup with Opine and React in Deno.
TypeScript
1
star
20

denocl

TypeScript
1
star
21

readme-runner

Run code snippets from a README (or any other file).
TypeScript
1
star
22

oceanic-next

Oceanic-Next Theme for Native Mac Terminal
1
star
23

applicationinsights-express-middleware

Express Middleware tracking for Microsoft Application Insights SDK for Node.js.
JavaScript
1
star