@natlibfi/fixugen-http-client
v4.0.1-alpha.1
Published
Generate unit tests for HTTP clients with fixugen
Downloads
861
Readme
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.