@natlibfi/fixugen-http-client

3.0.4 • Public • Published

Generate unit tests for HTTP clients with fixugen

Generate unit tests with fixugen and mocks HTTP responses nock. Useful for modules that export a Javascript interface for i.e. REST APIs.

Uses fixugen's useMetadataFile so your fixture directories must contain metadata.json file.

Usage

import 'fetch' from 'node-fetch';
import {expect} from 'chai';
import generateTests from '@natlibfi/fixugen-http-client';

generateTests({
  callback,
  path: [__dirname, '..', 'test-fixtures']
});

function callback({getFixture}) {
  // The base URL is always http://foo.bar
  const response = await fetch('http://foo.bar');
  expect(response.status).to.equal(200);
}

Configuration

The following optional properties are supported in the factory function:

  • mocha: Mocha options
  • fixura: Fixura options
  • recurse: Fixugen's recurse option. Defaults to true

metadata.json

An array property requests must be present in metadata.json file. It supports the following properties:

  • status: HTTP status code (Number). Mandatory.
  • method: HTTP method in lowercase. Mandatory.
  • url: URL of the request. This is only the location and parameters part of the actual URL. The base URL is always http://foo.bar. Must start with /. Mandatory.
  • requestHeaders: An object representing requests headers.
  • responseHeaders: An object representing response headers.

This configuration is also passed to the callback as the property requests.

Request and response payloads

The fixture directory for each unit test can have request- and response payload fixtures which must match the following filename pattern: /^request[0-9]+ /^response[0-9]+

Where [0-9]+ denotes the index number of the fixture (Requests and responses are mocked in that order).

License and copyright

Copyright (c) 2020 University Of Helsinki (The National Library Of Finland)

This project's source code is licensed under the terms of GNU Lesser General Public License Version 3 or any later version.

Readme

Keywords

none

Package Sidebar

Install

npm i @natlibfi/fixugen-http-client

Weekly Downloads

68

Version

3.0.4

License

LGPL-3.0+

Unpacked Size

35.1 kB

Total Files

13

Last publish

Collaborators

  • natlibfi-jonollil
  • drone-melinda