Class: ConfigurableResponses

ConfigurableResponses(responses, nameopt)

Handle returning a pre-configured responses.

This is one of the nullability patterns from James Shore's article on testing without mocks.

Example usage for stubbing fetch function:

function createFetchStub(responses) {
  const configurableResponses = ConfigurableResponses.create(responses);
  return async function () {
    const response = configurableResponses.next();
    return {
      status: response.status,
      json: async () => response.body,
    };
  };
}

Constructor

new ConfigurableResponses(responses, nameopt)

Creates a configurable responses instance from a single response or an array of responses with an optional response name.

Parameters:
Name Type Attributes Description
responses * | Array

A single response or an array of responses.

name string <optional>

An optional name for the responses.

Classes

ConfigurableResponses

Methods

next() → {*}

Returns the next response.

If there are no more responses, an error is thrown. If a single response is configured, it is always returned.

Returns:

The next response.

Type
*

(static) create(responses, nameopt)

Creates a configurable responses instance from a single response or an array of responses with an optional response name.

Parameters:
Name Type Attributes Description
responses * | Array

A single response or an array of responses.

name string <optional>

An optional name for the responses.