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.
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);
}
The following optional properties are supported in the factory function:
- mocha: Mocha options
- fixura: Fixura options
- recurse: Fixugen's recurse option. Defaults to true
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.
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).
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.