Papupata Documentation

API Reference

module invokeImplementationAdapter

Default export createInvokeImplementationAdapter

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 createInvokeImplementationAdapter from 'papupata/adapters/invokeImplementation'

Call the function with any options you wish and use the returned value as the requestAdapter of the APIDeclaration. As always, a base url needs to be set up to make calls, but its value is ultimately irrelevant when using this adapter.

Parameters

NameTypeDescription
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

import { APIDeclaration } from 'papupata'
import createInvokeImplementationAdapter from 'papupata/adapters/invokeImplementation'
import express from 'express'

const app = express()
const request = supertest(app)
const API = new APIDeclaration()
API.configure({
  app,
  baseURL: '',
  requestAdapter: createInvokeImplementationAdapter()
})