@efebia/jest-match-response 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @efebia/jest-match-response
|
|
2
|
+
|
|
3
|
+
Jest custom matchers for testing HTTP responses and JSON schemas with AJV.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @efebia/jest-match-response
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node.js >= 18 and Jest >= 28.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
Register the matchers in your Jest setup file:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// jest.setup.ts
|
|
19
|
+
import Ajv from 'ajv';
|
|
20
|
+
import { expect } from '@jest/globals';
|
|
21
|
+
import { buildToMatchJson, toMatchResponse } from '@efebia/jest-match-response';
|
|
22
|
+
|
|
23
|
+
const ajv = new Ajv();
|
|
24
|
+
|
|
25
|
+
expect.extend({
|
|
26
|
+
toMatchJson: buildToMatchJson({ ajv }),
|
|
27
|
+
toMatchResponse,
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// jest.config.js
|
|
33
|
+
module.exports = {
|
|
34
|
+
setupFilesAfterFramework: ['./jest.setup.ts'],
|
|
35
|
+
};
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### `toMatchResponse(statusCode, jsonSchema)`
|
|
41
|
+
|
|
42
|
+
Asserts that a response object has the expected status code **and** that its JSON body matches the given JSON Schema.
|
|
43
|
+
|
|
44
|
+
The received value must expose:
|
|
45
|
+
- `.status` or `.statusCode` — the HTTP status code
|
|
46
|
+
- `.json()` — a synchronous method returning the parsed response body
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const response = await app.inject({ method: 'GET', url: '/users/1' });
|
|
50
|
+
|
|
51
|
+
// passes if status is 200 and body matches the schema
|
|
52
|
+
expect(response).toMatchResponse(200, {
|
|
53
|
+
type: 'object',
|
|
54
|
+
required: ['id', 'name'],
|
|
55
|
+
properties: {
|
|
56
|
+
id: { type: 'string' },
|
|
57
|
+
name: { type: 'string' },
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// negative assertion
|
|
62
|
+
expect(response).not.toMatchResponse(404, {});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `toMatchJson(jsonSchema)`
|
|
66
|
+
|
|
67
|
+
Asserts that a value matches a JSON Schema directly.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
expect({ id: '1', name: 'Alice' }).toMatchJson({
|
|
71
|
+
type: 'object',
|
|
72
|
+
required: ['id', 'name'],
|
|
73
|
+
properties: {
|
|
74
|
+
id: { type: 'string' },
|
|
75
|
+
name: { type: 'string' },
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### `buildToMatchJson(options)`
|
|
81
|
+
|
|
82
|
+
Factory for creating a `toMatchJson` matcher with a custom AJV instance.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import Ajv from 'ajv';
|
|
86
|
+
import addFormats from 'ajv-formats';
|
|
87
|
+
|
|
88
|
+
const ajv = new Ajv();
|
|
89
|
+
addFormats(ajv);
|
|
90
|
+
|
|
91
|
+
expect.extend({ toMatchJson: buildToMatchJson({ ajv }) });
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| Option | Type | Description |
|
|
95
|
+
|--------|------|-------------|
|
|
96
|
+
| `ajv` | `Ajv` | The AJV instance used to compile and validate schemas |
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|