# @natlibfi/fixugen-http-client

> Generate unit tests for HTTP clients with fixugen

Latest version **5.0.0** (published 2026-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @natlibfi/fixugen-http-client
pnpm add @natlibfi/fixugen-http-client
yarn add @natlibfi/fixugen-http-client
bun add @natlibfi/fixugen-http-client
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-10-02 |
| First published | 2020-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24 |
| Dependencies | 4 |
| Unpacked size | 23.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | The National Library of Finland |
| Maintainers | drone-melinda, natlibfi-jonollil |

## Links

- npm: https://www.npmjs.com/package/@natlibfi/fixugen-http-client
- Repository: https://github.com/NatLibFi/fixugen-http-client-js
- Homepage: https://www.npmjs.com/package/@natlibfi/fixugen-http-client
- Issues: https://github.com/natlibfi/fixugen-http-client-js/issues
- npm.io page: https://npm.io/package/@natlibfi/fixugen-http-client

## Dependencies (4)

- [nock](https://npm.io/package/nock.md) ^14.0.12
- [debug](https://npm.io/package/debug.md) ^4.4.3
- [@natlibfi/fixura](https://npm.io/package/@natlibfi/fixura.md) ^5.0.0
- [@natlibfi/fixugen](https://npm.io/package/@natlibfi/fixugen.md) ^4.0.0

## Recent versions

- 5.0.0 (latest) — 2026-10-02
- 5.0.0-alpha.1 (next) — 2026-04-14
- 4.0.3 — 2026-03-05
- 4.0.3-alpha.1 — 2026-03-05
- 4.0.2 — 2026-01-12
- 4.0.2-- — 2026-01-12
- 4.0.2-alpha.1 — 2026-01-12
- 4.0.1 — 2025-12-11
- 4.0.0 — 2025-11-05
- 4.0.0-alpha.2 — 2025-10-22
- 4.0.0-alpha.1 — 2025-08-07
- 2.0.25-alpha.1 — 2025-06-26
- 3.0.11 — 2025-06-26
- 3.0.11-alpha.1 — 2025-06-26
- 4.0.1-alpha.1 — 2024-10-31
- … 37 more at https://npm.io/package/@natlibfi/fixugen-http-client/versions

## README

# Generate unit tests for HTTP clients with fixugen

Generate unit tests with fixugen and mocks HTTP responses [nock](https://www.npmjs.com/package/nock). Useful for modules that export a Javascript interface for i.e. REST APIs.

Uses [fixugen's](https://www.npmjs.com/package/@natlibfi/fixugen) **useMetadataFile** so your fixture directories must contain **metadata.json** file.

# Usage
```js
import assert from 'node:assert';
import generateTests from '@natlibfi/fixugen-http-client';

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

function callback({getFixture}) {
  // The base URL is always http://foo.bar
  const response = await fetch('http://foo.bar');
  assert.equal(response.status, 200);
}
```
# Configuration
The following optional properties are supported in the factory function:
- **hooks**: Timed hook options (before, beforeEach, after, afterEach)
- **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**.
- **query**: An object representing requests query parameters (Note: these can also be given as string as part of **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 and rest of the request do not have request or response payload fixtures available.)

## License and copyright

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

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

---
_Source: https://npm.io/package/@natlibfi/fixugen-http-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
