npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-test

v0.0.6

Published

testing framework for koa using supertest and tape-async

Downloads

15

Readme

koa-test

Easy to use framework for koa API testing built on tape-async and supertest. The purpose of this project is to make testing for APIs as descriptive as possible.

Installation

npm i -D koa-test

Usage

Set up an scenario that will only pass only when all your assertions pass.

Simple API Testing

const createTestSuite = require('koa-test');
const koa = require('koa');

function apiFactory() {
  return koa()
    .use(function* helloWorld(next) {
      this.body = 'Hello World!';
      yield next;
    });
}

const createScenario = createTestSuite(apiFactory);

createScenario(
  {
    title: 'GET /',
    path: '/',
    method: 'get',
    assertions: [
      (res, t) => t.equal(res.status, 200),
      (res, t) => t.equal(res.text, 'Hello World!');
    ],
  },
);

You can also take advantage of dependency injection in order to easily unit test your API (or pass the real thing and do some integration testing if you are into that).

const createTestSuite = require('koa-test');
const koa = require('koa');

function apiFactory(dependencies) {
  const { returnHelloWorld } = dependencies;

  return koa()
    .use(function* helloWorld(next) {
      this.body = returnHelloWorld();
      yield next;
    });
}

const createScenario = createTestSuite(apiFactory);

createScenario(
  {
    title: 'GET /',
    path: '/',
    method: 'get',
    assertions: [
      (res, t) => t.equal(res.status, 200),
      (res, t) => t.equal(res.text, 'Hello World from a dependency!');
    ],
  },
  {
    returnHelloWorld: () => 'Hello World from a dependency!';
  }
);

API

createTestSuite

createTestSuite takes in a function that returns a koa app and returns a createScenario function that allows you to build your testing scenarios.

function apiFactory() {
  return koa()
    .use(function* helloWorld(next) {
      this.body = 'Hello World!';
      yield next;
    });
}

const createScenario = createTestSuite(apiFactory);

createScenario

createScenario takes two objects: config which is the setup for the request you will send to your API and dependencies which is an optional parameter containing all the dependencies you want to pass to your factory.

createScenario(
  {
    title: 'GET /',
    path: '/',
    method: 'get',
    assertions: [
      (res, t) => t.equal(res.status, 200),
      (res, t) => t.equal(res.text, 'Hello World from a dependency!');
    ],
  },
  {
    returnHelloWorld: () => 'Hello World from a dependency!';
  }
);

config

The config object has several fields that are used to build your request to your API.

  • title: The title that you will see int the TAP output on tape.
  • path: The path that your request will write to. (required)
  • method: The method which will be used on the request. (required)
  • body: The payload of the request.
  • query: Object containing the query parameters of the request.
  • headers: Object containing all the headers of the request.
  • assertions: Array of functions that will receive tape's test object and the response object retreived from request.

assertion

An assertion function is callback that accepts two parameters: res which is the whole response object returned by supertest and t which is the whole assertion object created by tape.

(res, t) => t.equals(res.status, 200);

dependencies

The dependencies object is optional and it only serves the purpose to provide dependency injection to your API.