Papupata Documentation

API Reference

module testInvoker

Default export testInvoke

Purpose

This module allows you to use invoke implemented APIs with no express server. This is primarily used for testing.

Availability

This functionality is available from papupata version 1.5.0 onwards.

Usage

import testInvoke from 'papupata/invokers/test'

Call the function with the DeclaredAPI you wish to invoke and any parameters you wish to pass to it, as if you were calling it normally.

Parameters

NameTypeDescription
apiDeclaredAPIThe API you wish to invoke
argsObjectArguments to the API. Non-object bodies are not supported.
optionsObject?

Members

NameTypeData type/return typeDescriptionRequired
createRequestmethod(requestBase) => Request

This function is used to create the request passed to the API as if it was the express request. Its sole parameter is a very minimal version of the request, which you may use as a template.

This method can be used to add any necessary fields to the request.

const createRequest = base => ({...base, headers: { 'content-type': 'text/html' }})
No
createResponsemethod(responseBase) => Response

This function is used to create the response passed to the API as if it was the express response. Its sole parameter a wrapper papupata uses to handle the response, which can be extended with other properties and methods as necessary.

const createResponse = base => ({...base, myField: true})
No
assertResponsemethod(response) => void

This function is called once the response is complete. It is passed a mock express response, allowing for making assertions to whatever it may contain.

const assertResponse = res => expect(res.statusCode).toEqual(400)
No
withMiddlewarepropertyboolean

By default, and if explicitly set to false only the API implementation is called and all middleware is ignored.

If set to true, any express and papupata middleware on the route is run as normal for the requests.

No

Returns

Papupata MakeRequestAdapter

Examples

Basic case
import { APIDeclaration } from 'papupata'
import testInvoke from 'papupata/invokers/test'
import express from 'express'

const app = express()
const request = supertest(app)
const API = new APIDeclaration()
const api = API.declareGetAPI('/').query({id: String}).response<string>()

testInvoke(api, {id: '123'})
With options
testInvoke(api, {id: '123'}, {withMiddleware: true})