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 = ;const koa = ; { return ;} const createScenario = ; ;
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 = ;const koa = ; { const returnHelloWorld = dependencies; return ;} const createScenario = ; ;
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.
{ return ;} const createScenario = ;
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.
;
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'stest
object and theresponse
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.
t;
dependencies
The dependencies
object is optional and it only serves the purpose to provide dependency injection to your API.