# @janiscommerce/api-test

> A package for testing APIs developed with @janiscommerce/api

Latest version **6.2.0** (published 2026-10-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/api-test
pnpm add @janiscommerce/api-test
yarn add @janiscommerce/api-test
bun add @janiscommerce/api-test
```

## Health

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

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 6.2.0 |
| Published | 2026-10-22 |
| First published | 2019-07-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 3 |
| Unpacked size | 20.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/api-test
- Repository: https://github.com/janis-commerce/api-test
- Homepage: https://github.com/janis-commerce/api-test.git#readme
- Issues: https://github.com/janis-commerce/api-test/issues
- npm.io page: https://npm.io/package/@janiscommerce/api-test

## Dependencies (3)

- [sinon](https://npm.io/package/sinon.md) ^15.0.3
- [@janiscommerce/api](https://npm.io/package/@janiscommerce/api.md) ^8.3.0
- [@janiscommerce/api-session](https://npm.io/package/@janiscommerce/api-session.md) ^3.4.0

## Recent versions

- 6.2.0 (latest) — 2026-10-22
- 7.0.0-1 (next) — 2025-03-31
- 6.1.0 — 2025-12-16
- 7.0.0-0 — 2025-03-31
- 6.0.0 — 2023-09-18
- 5.0.0 — 2023-04-18
- 4.3.0 — 2023-01-16
- 4.2.1 — 2022-08-02
- 4.2.0 — 2022-08-02
- 4.1.1 — 2021-11-12
- 4.1.0 — 2021-11-11
- 4.0.0 — 2020-08-27
- 3.1.0 — 2020-08-19
- 3.0.1 — 2020-06-16
- 3.0.0 — 2020-06-16
- … 14 more at https://npm.io/package/@janiscommerce/api-test/versions

## README

# api-test

![Build Status](https://github.com/janis-commerce/api-test/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/api-test/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/api-test?branch=master)
[![npm version](https://badge.fury.io/js/%40janiscommerce%2Fapi-test.svg)](https://www.npmjs.com/package/@janiscommerce/api-test)

A package for testing APIs developed with [@janiscommerce/api](https://www.npmjs.com/package/@janiscommerce/api).
`api-test` should be used for testing purposes only, because allows you to test your APIs.

## Installation
```sh
npm install @janiscommerce/api-test --save-dev
```

## Usage
`api-test` is called with a function that receives an API class and an Array of rules.
For each rule, the module will create an `it()` block for creating an individual test.

```js
const APITest = require('@janiscommerce/api-test');
const MyAPIClass = require('path/to/my-api');

describe('MyAPI Tests', () => {

	APITest(MyAPIClass, [{
		description: 'should do something when other thing happend',
		request: {
			data: { foo: 123 }
		},
		response: {
			code: 200, // default
			body: { result: 1 }
		}
	}]);

});
```
## API Parameters
The api tester can receive 3 parameters
`MyAPIClass[, endpoint], rules`

- **MyAPIClass** *object*
The class to test

- **endpoint** *string* *[Optional]*
The endpoint that should use to run the test

- **rules** *array*
Array of test that will run individual.

```js
const APITest = require('@janiscommerce/api-test');
const MyAPIClass = require('path/to/my-api');

describe('MyAPI Tests', () => {

	APITest(MyAPIClass, '/path/to/my-api', [{
		description: 'should do something when other thing happend',
		request: {
			data: { foo: 123 },
			endpoint: '/custom/endpoint/for/current/test'
		},
		response: {
			code: 200, // default
			body: { result: 1 }
		}
	}]);

});
```

## Rule components
A rule is each test that will run individual.
These are the components of a rule.

- **description** *string*
The text that will be added in the `it()` function. This fields is required.

- **only** *boolean*
If it's set to true, only this rule will be executed. Useful to debug when a test fails.

- **session** *object || boolean*
An object with the API session, or `true` to set a default session. If it's falsy, it won't inject the session. For session details see [@janiscommerce/api-session](https://www.npmjs.com/package/@janiscommerce/api-session)

- **client** *object*
An object with the client that the session would fetch from BM in `client` getter. It it's not set, a default client is used. For session getters details see [@janiscommerce/api-session](https://www.npmjs.com/package/@janiscommerce/api-session)

- **isExport** *boolean* _[Since 6.1.0]_
Set to true to indicate that the API is used for exporting with Batch new Export feature.

- **request** *object*
An object with the request data. This field is

- **request.data** *object*
Optional data that the API will received.

- **request.endpoint** *string*
Optional endpoint that the execution has to use for that specific rule.

- **request.pathParameters** *object*
Optional pathParameters that the API will received.

- **request.headers** *object*
Optional headers that the API will received.

- **request.cookies** *object*
Optional cookies that the API will received.

- **request.sourceIp** *string* _[Since 6.2.0]_
Optional source IP of the request that the API will received in the `sourceIp` getter. If it's not set, the default `127.0.0.1` is used. Requires [@janiscommerce/api](https://www.npmjs.com/package/@janiscommerce/api) 8.3.0 or greater.

- **response** *object*
The response. This field is required.

- **response.code** *number*
The response code expected. The default value is `200`.

- **response.headers** *object*
The response headers expected. This field is not strict. Will check each header individually.

- **response.strictHeaders** *object*
The response headers expected. This field is strict. Will check all headers given against all headers that the API will respond.

- **response.cookies** *object*
The response cookies expected. This field is not strict. Will check each cookie individually.

- **response.strictCookies** *object*
The response cookies expected. This field is strict. Will check all cookies given against all headers that the API will respond.

- **before** *function*
An optional function for configuring mocks or stubbing some things.
Receives a [sinon sandbox](https://sinonjs.org/releases/latest/sandbox/), this sanbox will be restored after each test (with `afterEach()` :wink:).
Example:
```js
{
	description: 'should get using the model and return 200 http response code',
	before: sandbox => {
		sandbox.stub(SomeModel.prototype, 'get');
	},
	response: { code: 200 }
};
```

- **getResponse** *function*
An optional function. Returns the response before the assertions. This is helpful for debugging reasons.
Example:
```js
{
	description: 'should return 200',
	getResponse: response => {
		console.log(response); // use this code for debugging if your test fails!
	},
	response: { code: 200 }
};
```

- **after** *function*
An optional function for testing additional stuff.
Receives the API response object and the [sinon sandbox](https://sinonjs.org/releases/latest/sandbox/).
Example:
```js
{
	description: 'should get using the model and return the formatted items',
	request: {
		data: { id: 1 }
	},
	before: sandbox => {
		sandbox.stub(SomeModel.prototype, 'get')
			.returns([{ foo: 1 }, { bar: 2 }]);
	},
	response: {
		body: [{
			foo: 1,
			formattedByAPI: true
		}, {
			bar: 2,
			formattedByAPI: true
		}]
	},
	after: (response, sandbox) => {
		sandbox.assert.calledOnce(SomeModel.prototype.get);
		sandbox.assert.calledWithExactly(SomeModel.prototype.get.getCall(0), { id: 1 });

		// do somthing nice with response...
	}
};
```

## Rule validation errors
When you configure a rule, `api-test` will throw an `APITestError` if there are a validation error.
These are the possible validation errors.

|Code|Description|
|--|--|
|1|Invalid Rules. It means that you didn't sent an array of rules.|
|2|Invalid Rule format. It means that you didn't sent an object for a rule.|
|3|Invalid Rule description. It means that the description is missing or isn't a string.|
|4|Invalid Rule request. It means that the request you sent was not an object.|
|5|Invalid Rule response. It means that the response is missing or was not an object.|
|6|Invalid Rule response code. It means that the response code you sent was not a number.|
|7|Invalid Rule response headers. It means that the response headers you sent was not an object.|
|8|Invalid Rule response cookies. It means that the response cookies you sent was not an object.|
|9|Invalid Rule session. It means that the API Session you sent was not an object nor a boolean.|
|10|Invalid Rule client. It means that the Client you sent was not an object.|

---
_Source: https://npm.io/package/@janiscommerce/api-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
