4.0.1-alpha.1 • Published 8 months ago

@natlibfi/fixugen-http-client v4.0.1-alpha.1

Weekly downloads
11
License
MIT
Repository
github
Last release
8 months ago

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.

NOTE: from version 4.0.0 onwards fixugen-http-client-js uses undici insteads of nock and won't work for testing stuff that uses node-fetch instead of node native fetch

Usage

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 uppercase. 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.
  • query: An object representing requests query parameters (Note: these can also be given as string in url)
  • 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 order in which requests and responses are mocked (at least for single digits). Note that if unit test has several requests, request and response payload fixtures are matched to these in order, regardless of the digit used in file name. (Ie if we have three requests and request1.txt and response3.txt fixtures, both request1.txt and response3.txt get mapped to first request.)

License and copyright

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

This project's source code is licensed under the terms of MIT or any later version.

4.0.1-alpha.2

8 months ago

4.0.1-alpha.1

8 months ago

3.0.10-alpha.1

9 months ago

3.0.10

9 months ago

3.0.9

11 months ago

3.0.8

11 months ago

3.0.9-alpha.1

11 months ago

3.0.8-alpha.1

11 months ago

3.0.7

1 year ago

3.0.7-alpha.1

1 year ago

3.0.6

1 year ago

3.0.6-alpha.1

1 year ago

3.0.5-alpha.2

1 year ago

3.0.5-alpha.1

1 year ago

3.0.5

1 year ago

3.0.4-alpha.1

1 year ago

3.0.4

1 year ago

3.0.3

2 years ago

3.0.3-alpha.1

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

3.0.0-alpha.1

2 years ago

3.0.1-alpha.2

2 years ago

3.0.2-alpha.1

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago