@ehuelsmann/jest-openapi 0.15.0 → 0.16.0
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 +14 -8
- package/dist/index.d.mts +21 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +244 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +243 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -12
- package/dist/index.d.ts.map +0 -1
- package/dist/matchers/toSatisfyApiSpec.d.ts +0 -3
- package/dist/matchers/toSatisfyApiSpec.d.ts.map +0 -1
- package/dist/matchers/toSatisfyApiSpec.js +0 -81
- package/dist/matchers/toSatisfyApiSpec.js.map +0 -1
- package/dist/matchers/toSatisfySchemaInApiSpec.d.ts +0 -3
- package/dist/matchers/toSatisfySchemaInApiSpec.d.ts.map +0 -1
- package/dist/matchers/toSatisfySchemaInApiSpec.js +0 -36
- package/dist/matchers/toSatisfySchemaInApiSpec.js.map +0 -1
- package/dist/utils.d.ts +0 -3
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -9
- package/dist/utils.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
# @ehuelsmann/jest-openapi
|
|
2
2
|
|
|
3
|
+
> **Notice:** Support for responses from [`request`](https://github.com/request/request#deprecated) and [`request-promise`](https://github.com/request/request-promise#deprecated) has been removed because both upstream projects are deprecated.
|
|
4
|
+
|
|
3
5
|
[](https://www.npmjs.com/package/@ehuelsmann/jest-openapi)
|
|
4
6
|
[](https://www.npmjs.com/package/@ehuelsmann/jest-openapi)
|
|
5
|
-

|
|
6
8
|

|
|
7
|
-
[](https://github.com/
|
|
9
|
-
[](https://github.com/
|
|
9
|
+
[](https://codecov.io/gh/ehuelsmann/OpenAPIValidators)
|
|
10
|
+
[](https://github.com/ehuelsmann/OpenAPIValidators/blob/master/packages/jest-openapi/src/index.ts)
|
|
11
|
+
[](https://github.com/ehuelsmann/OpenAPIValidators/blob/master/CONTRIBUTING.md)
|
|
10
12
|
|
|
11
13
|
Additional Jest matchers for asserting that HTTP responses satisfy an OpenAPI spec.
|
|
12
14
|
|
|
15
|
+
**Note** This project is a fork from the [openapi-library](https://github.com/openapi-library) project because that project is not maintained at the moment. This project contains updates and security fixes, but ideally will be merged back into that project, as soon as it's maintained again.
|
|
16
|
+
|
|
13
17
|
## Problem 😕
|
|
14
18
|
|
|
15
19
|
If your server's behaviour doesn't match your API documentation, then you need to correct your server, your documentation, or both. The sooner you know the better.
|
|
@@ -27,12 +31,12 @@ Features:
|
|
|
27
31
|
- Supports OpenAPI specs in YAML and JSON formats
|
|
28
32
|
- Supports `$ref` in response definitions (i.e. `$ref: '#/definitions/ComponentType/ComponentName'`)
|
|
29
33
|
- Informs you if your OpenAPI spec is invalid
|
|
30
|
-
- Supports responses from `axios`, `
|
|
31
|
-
- Use in [Jest](#usage), or use our [sister package](https://github.com/
|
|
34
|
+
- Supports responses from `axios`, `supertest`, `superagent`, and `chai-http`
|
|
35
|
+
- Use in [Jest](#usage), or use our [sister package](https://github.com/ehuelsmann/OpenAPIValidators/tree/master/packages/chai-openapi-response-validator#readme) for Mocha and other test runners that support Chai
|
|
32
36
|
|
|
33
37
|
## Contributing ✨
|
|
34
38
|
|
|
35
|
-
If you've come here to help contribute - thanks! Take a look at the [contributing](https://github.com/
|
|
39
|
+
If you've come here to help contribute - thanks! Take a look at the [contributing](https://github.com/ehuelsmann/OpenAPIValidators/blob/master/CONTRIBUTING.md) docs to get started.
|
|
36
40
|
|
|
37
41
|
## Installation
|
|
38
42
|
|
|
@@ -50,7 +54,9 @@ yarn add --dev @ehuelsmann/jest-openapi
|
|
|
50
54
|
|
|
51
55
|
## Importing
|
|
52
56
|
|
|
53
|
-
|
|
57
|
+
This package ships with both **ESM** (preferred) and **CommonJS** builds, so it works in any environment.
|
|
58
|
+
|
|
59
|
+
ESM / TypeScript
|
|
54
60
|
|
|
55
61
|
```typescript
|
|
56
62
|
import jestOpenAPI from '@ehuelsmann/jest-openapi';
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { OpenAPISpecObject } from '@ehuelsmann/openapi-validator';
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
namespace jest {
|
|
5
|
+
interface Matchers<R> {
|
|
6
|
+
/**
|
|
7
|
+
* Check the HTTP response object satisfies a response defined in your OpenAPI spec.
|
|
8
|
+
* [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-api-tests-validate-the-status-and-body-of-http-responses-against-your-openapi-spec)
|
|
9
|
+
*/
|
|
10
|
+
toSatisfyApiSpec(): R;
|
|
11
|
+
/**
|
|
12
|
+
* Check the object satisfies a schema defined in your OpenAPI spec.
|
|
13
|
+
* [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-unit-tests-validate-objects-against-schemas-defined-in-your-openapi-spec)
|
|
14
|
+
*/
|
|
15
|
+
toSatisfySchemaInApiSpec(schemaName: string): R;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
declare function export_default(filepathOrObject: string | OpenAPISpecObject): void;
|
|
20
|
+
|
|
21
|
+
export { export_default as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OpenAPISpecObject } from '@ehuelsmann/openapi-validator';
|
|
2
|
+
|
|
2
3
|
declare global {
|
|
3
4
|
namespace jest {
|
|
4
5
|
interface Matchers<R> {
|
|
@@ -15,5 +16,6 @@ declare global {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
declare function export_default(filepathOrObject: string | OpenAPISpecObject): void;
|
|
20
|
+
|
|
21
|
+
export { export_default as default };
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,250 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
4
9
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_openapi_validator2 = require("@ehuelsmann/openapi-validator");
|
|
27
|
+
|
|
28
|
+
// src/matchers/toSatisfyApiSpec.ts
|
|
29
|
+
var import_jest_matcher_utils = require("jest-matcher-utils");
|
|
30
|
+
var import_openapi_validator = require("@ehuelsmann/openapi-validator");
|
|
31
|
+
|
|
32
|
+
// src/utils.ts
|
|
33
|
+
var import_util = require("util");
|
|
34
|
+
var stringify = (obj) => (0, import_util.inspect)(obj, { showHidden: false, depth: null });
|
|
35
|
+
var joinWithNewLines = (...lines) => lines.join("\n\n");
|
|
36
|
+
|
|
37
|
+
// src/matchers/toSatisfyApiSpec.ts
|
|
38
|
+
function toSatisfyApiSpec_default(received, openApiSpec) {
|
|
39
|
+
const actualResponse = (0, import_openapi_validator.makeResponse)(received);
|
|
40
|
+
const validationError = openApiSpec.validateResponse(actualResponse);
|
|
41
|
+
const pass = !validationError;
|
|
42
|
+
const matcherHintOptions = {
|
|
43
|
+
comment: "Matches 'received' to a response defined in your API spec, then validates 'received' against it",
|
|
44
|
+
...this.isNot !== void 0 && { isNot: this.isNot },
|
|
45
|
+
...this.promise !== void 0 && { promise: this.promise }
|
|
46
|
+
};
|
|
47
|
+
const hint = (0, import_jest_matcher_utils.matcherHint)(
|
|
48
|
+
"toSatisfyApiSpec",
|
|
49
|
+
void 0,
|
|
50
|
+
"",
|
|
51
|
+
matcherHintOptions
|
|
52
|
+
);
|
|
53
|
+
const message = pass ? () => getExpectReceivedNotToSatisfyApiSpecMsg(
|
|
54
|
+
actualResponse,
|
|
55
|
+
openApiSpec,
|
|
56
|
+
hint
|
|
57
|
+
) : () => getExpectReceivedToSatisfyApiSpecMsg(
|
|
58
|
+
actualResponse,
|
|
59
|
+
openApiSpec,
|
|
60
|
+
validationError,
|
|
61
|
+
hint
|
|
62
|
+
);
|
|
63
|
+
return {
|
|
64
|
+
pass,
|
|
65
|
+
message
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function getExpectReceivedToSatisfyApiSpecMsg(actualResponse, openApiSpec, validationError, hint) {
|
|
69
|
+
const { status, req } = actualResponse;
|
|
70
|
+
const { method, path: requestPath } = req;
|
|
71
|
+
const unmatchedEndpoint = `${method} ${requestPath}`;
|
|
72
|
+
if (validationError.code === import_openapi_validator.ErrorCode.ServerNotFound) {
|
|
73
|
+
return joinWithNewLines(
|
|
74
|
+
hint,
|
|
75
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
76
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} had request path ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(requestPath)}, but your API spec has no matching servers`,
|
|
77
|
+
`Servers found in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(openApiSpec.getServerUrls().join(", "))}`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
if (validationError.code === import_openapi_validator.ErrorCode.BasePathNotFound) {
|
|
81
|
+
return joinWithNewLines(
|
|
82
|
+
hint,
|
|
83
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
84
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} had request path ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(requestPath)}, but your API spec has basePath ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(openApiSpec.spec.basePath)}`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (validationError.code === import_openapi_validator.ErrorCode.PathNotFound) {
|
|
88
|
+
const pathNotFoundErrorMessage = joinWithNewLines(
|
|
89
|
+
hint,
|
|
90
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
91
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} had request path ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(requestPath)}, but your API spec has no matching path`,
|
|
92
|
+
`Paths found in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(openApiSpec.paths().join(", "))}`
|
|
93
|
+
);
|
|
94
|
+
if ("didUserDefineBasePath" in openApiSpec && openApiSpec.didUserDefineBasePath) {
|
|
95
|
+
return joinWithNewLines(
|
|
96
|
+
pathNotFoundErrorMessage,
|
|
97
|
+
`'${requestPath}' matches basePath \`${openApiSpec.spec.basePath}\` but no <basePath/endpointPath> combinations`
|
|
98
|
+
);
|
|
24
99
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
100
|
+
if ("didUserDefineServers" in openApiSpec && openApiSpec.didUserDefineServers) {
|
|
101
|
+
return joinWithNewLines(
|
|
102
|
+
pathNotFoundErrorMessage,
|
|
103
|
+
`'${requestPath}' matches servers ${stringify(
|
|
104
|
+
openApiSpec.getMatchingServerUrls(requestPath)
|
|
105
|
+
)} but no <server/endpointPath> combinations`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return pathNotFoundErrorMessage;
|
|
109
|
+
}
|
|
110
|
+
const path = openApiSpec.findOpenApiPathMatchingRequest(req);
|
|
111
|
+
const endpoint = `${method} ${path}`;
|
|
112
|
+
if (validationError.code === import_openapi_validator.ErrorCode.MethodNotFound) {
|
|
113
|
+
const expectedPathItem = openApiSpec.findExpectedPathItem(req);
|
|
114
|
+
const expectedRequestOperations = Object.keys(expectedPathItem).map((operation) => operation.toUpperCase()).join(", ");
|
|
115
|
+
return joinWithNewLines(
|
|
116
|
+
hint,
|
|
117
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
118
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} had request method ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(method)}, but your API spec has no ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(method)} operation defined for path '${path}'`,
|
|
119
|
+
`Request operations found for path '${path}' in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(expectedRequestOperations)}`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (validationError.code === import_openapi_validator.ErrorCode.StatusNotFound) {
|
|
123
|
+
const expectedResponseOperation = (
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
125
|
+
openApiSpec.findExpectedResponseOperation(req)
|
|
126
|
+
);
|
|
127
|
+
const expectedResponseStatuses = Object.keys(
|
|
128
|
+
expectedResponseOperation.responses
|
|
129
|
+
).join(", ");
|
|
130
|
+
return joinWithNewLines(
|
|
131
|
+
hint,
|
|
132
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
133
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} had status ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(status)}, but your API spec has no ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(status)} response defined for endpoint '${endpoint}'`,
|
|
134
|
+
`Response statuses found for endpoint '${endpoint}' in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(expectedResponseStatuses)}`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
138
|
+
return joinWithNewLines(
|
|
139
|
+
hint,
|
|
140
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
141
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} did not satisfy it because: ${validationError}`,
|
|
142
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} contained: ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(actualResponse.toString())}`,
|
|
143
|
+
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(stringify(responseDefinition))}`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
function getExpectReceivedNotToSatisfyApiSpecMsg(actualResponse, openApiSpec, hint) {
|
|
147
|
+
const { status, req } = actualResponse;
|
|
148
|
+
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
149
|
+
const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(
|
|
150
|
+
req
|
|
151
|
+
)}`;
|
|
152
|
+
return joinWithNewLines(
|
|
153
|
+
hint,
|
|
154
|
+
`expected ${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
155
|
+
`${(0, import_jest_matcher_utils.RECEIVED_COLOR)("received")} contained: ${(0, import_jest_matcher_utils.RECEIVED_COLOR)(actualResponse.toString())}`,
|
|
156
|
+
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${(0, import_jest_matcher_utils.EXPECTED_COLOR)(stringify(responseDefinition))}`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/matchers/toSatisfySchemaInApiSpec.ts
|
|
161
|
+
var import_jest_matcher_utils2 = require("jest-matcher-utils");
|
|
162
|
+
function toSatisfySchemaInApiSpec_default(received, schemaName, openApiSpec) {
|
|
163
|
+
const matcherHintOptions = {
|
|
164
|
+
comment: "Matches 'received' to a schema defined in your API spec, then validates 'received' against it",
|
|
165
|
+
...this.isNot !== void 0 && { isNot: this.isNot },
|
|
166
|
+
...this.promise !== void 0 && { promise: this.promise }
|
|
167
|
+
};
|
|
168
|
+
const hint = (0, import_jest_matcher_utils2.matcherHint)(
|
|
169
|
+
"toSatisfySchemaInApiSpec",
|
|
170
|
+
void 0,
|
|
171
|
+
"schemaName",
|
|
172
|
+
matcherHintOptions
|
|
173
|
+
);
|
|
174
|
+
const schema = openApiSpec.getSchemaObject(schemaName);
|
|
175
|
+
if (!schema) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
(0, import_jest_matcher_utils2.matcherErrorMessage)(
|
|
178
|
+
hint,
|
|
179
|
+
`${(0, import_jest_matcher_utils2.EXPECTED_COLOR)("schemaName")} must match a schema in your API spec`,
|
|
180
|
+
(0, import_jest_matcher_utils2.printWithType)("schemaName", schemaName, import_jest_matcher_utils2.printExpected)
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
const validationError = openApiSpec.validateObject(received, schema);
|
|
185
|
+
const pass = !validationError;
|
|
186
|
+
const message = pass ? () => getExpectReceivedNotToSatisfySchemaInApiSpecMsg(
|
|
187
|
+
received,
|
|
188
|
+
schemaName,
|
|
189
|
+
schema,
|
|
190
|
+
hint
|
|
191
|
+
) : () => getExpectReceivedToSatisfySchemaInApiSpecMsg(
|
|
192
|
+
received,
|
|
193
|
+
schemaName,
|
|
194
|
+
schema,
|
|
195
|
+
validationError,
|
|
196
|
+
hint
|
|
197
|
+
);
|
|
198
|
+
return {
|
|
199
|
+
pass,
|
|
200
|
+
message
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function getExpectReceivedToSatisfySchemaInApiSpecMsg(received, schemaName, schema, validationError, hint) {
|
|
204
|
+
return joinWithNewLines(
|
|
205
|
+
hint,
|
|
206
|
+
`expected ${(0, import_jest_matcher_utils2.RECEIVED_COLOR)("received")} to satisfy the '${schemaName}' schema defined in your API spec`,
|
|
207
|
+
`${(0, import_jest_matcher_utils2.RECEIVED_COLOR)("received")} did not satisfy it because: ${validationError}`,
|
|
208
|
+
`${(0, import_jest_matcher_utils2.RECEIVED_COLOR)("received")} was: ${(0, import_jest_matcher_utils2.RECEIVED_COLOR)(stringify(received))}`,
|
|
209
|
+
`The '${schemaName}' schema in API spec: ${(0, import_jest_matcher_utils2.EXPECTED_COLOR)(stringify(schema))}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
function getExpectReceivedNotToSatisfySchemaInApiSpecMsg(received, schemaName, schema, hint) {
|
|
213
|
+
return joinWithNewLines(
|
|
214
|
+
hint,
|
|
215
|
+
`expected ${(0, import_jest_matcher_utils2.RECEIVED_COLOR)("received")} not to satisfy the '${schemaName}' schema defined in your API spec`,
|
|
216
|
+
`${(0, import_jest_matcher_utils2.RECEIVED_COLOR)("received")} was: ${(0, import_jest_matcher_utils2.RECEIVED_COLOR)(stringify(received))}`,
|
|
217
|
+
`The '${schemaName}' schema in API spec: ${(0, import_jest_matcher_utils2.EXPECTED_COLOR)(stringify(schema))}`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/index.ts
|
|
222
|
+
function index_default(filepathOrObject) {
|
|
223
|
+
const openApiSpec = (0, import_openapi_validator2.makeApiSpec)(filepathOrObject);
|
|
224
|
+
const jestMatchers = {
|
|
225
|
+
toSatisfyApiSpec(received) {
|
|
226
|
+
return toSatisfyApiSpec_default.call(this, received, openApiSpec);
|
|
227
|
+
},
|
|
228
|
+
toSatisfySchemaInApiSpec(received, schemaName) {
|
|
229
|
+
return toSatisfySchemaInApiSpec_default.call(
|
|
230
|
+
this,
|
|
231
|
+
received,
|
|
232
|
+
schemaName,
|
|
233
|
+
openApiSpec
|
|
234
|
+
);
|
|
32
235
|
}
|
|
33
|
-
|
|
236
|
+
};
|
|
237
|
+
const jestExpect = global.expect;
|
|
238
|
+
if (jestExpect !== void 0) {
|
|
239
|
+
jestExpect.extend(jestMatchers);
|
|
240
|
+
} else {
|
|
241
|
+
console.error(
|
|
242
|
+
[
|
|
243
|
+
"Unable to find Jest's global expect.",
|
|
244
|
+
"Please check you have configured jest-openapi correctly.",
|
|
245
|
+
"See https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#usage for help."
|
|
246
|
+
].join("\n")
|
|
247
|
+
);
|
|
248
|
+
}
|
|
34
249
|
}
|
|
35
250
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAsBA,4BAiCC;AAvDD,qEAA+E;AAC/E,mFAA2D;AAC3D,mGAA2E;AAoB3E,mBAAyB,gBAA4C;IACnE,MAAM,WAAW,GAAG,IAAA,+BAAW,EAAC,gBAAgB,CAAC,CAAC;IAElD,MAAM,YAAY,GAAyB;QACzC,gBAAgB,CAAC,QAAiB;YAChC,OAAO,0BAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;QAC5D,CAAC;QACD,wBAAwB,CAAC,QAAiB,EAAE,UAAkB;YAC5D,OAAO,kCAAwB,CAAC,IAAI,CAClC,IAAI,EACJ,QAAQ,EACR,UAAU,EACV,WAAW,CACZ,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,MAAM,UAAU,GAAI,MAAmC,CAAC,MAAM,CAAC;IAE/D,0BAA0B;IAC1B,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC7B,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC;SAAM,CAAC;QACN,sCAAsC;QACtC,OAAO,CAAC,KAAK,CACX;YACE,sCAAsC;YACtC,0DAA0D;YAC1D,4GAA4G;SAC7G,CAAC,IAAI,CAAC,IAAI,CAAC,CACb,CAAC;IACJ,CAAC;IACD,0BAA0B;AAC5B,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/matchers/toSatisfyApiSpec.ts","../src/utils.ts","../src/matchers/toSatisfySchemaInApiSpec.ts"],"sourcesContent":["import { makeApiSpec, OpenAPISpecObject } from '@ehuelsmann/openapi-validator';\nimport toSatisfyApiSpec from './matchers/toSatisfyApiSpec';\nimport toSatisfySchemaInApiSpec from './matchers/toSatisfySchemaInApiSpec';\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace jest {\n interface Matchers<R> {\n /**\n * Check the HTTP response object satisfies a response defined in your OpenAPI spec.\n * [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-api-tests-validate-the-status-and-body-of-http-responses-against-your-openapi-spec)\n */\n toSatisfyApiSpec(): R;\n /**\n * Check the object satisfies a schema defined in your OpenAPI spec.\n * [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-unit-tests-validate-objects-against-schemas-defined-in-your-openapi-spec)\n */\n toSatisfySchemaInApiSpec(schemaName: string): R;\n }\n }\n}\n\nexport default function (filepathOrObject: string | OpenAPISpecObject): void {\n const openApiSpec = makeApiSpec(filepathOrObject);\n\n const jestMatchers: jest.ExpectExtendMap = {\n toSatisfyApiSpec(received: unknown) {\n return toSatisfyApiSpec.call(this, received, openApiSpec);\n },\n toSatisfySchemaInApiSpec(received: unknown, schemaName: string) {\n return toSatisfySchemaInApiSpec.call(\n this,\n received,\n schemaName,\n openApiSpec,\n );\n },\n };\n\n const jestExpect = (global as { expect?: jest.Expect }).expect;\n\n /* istanbul ignore next */\n if (jestExpect !== undefined) {\n jestExpect.extend(jestMatchers);\n } else {\n // eslint-disable-next-line no-console\n console.error(\n [\n \"Unable to find Jest's global expect.\",\n 'Please check you have configured jest-openapi correctly.',\n 'See https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#usage for help.',\n ].join('\\n'),\n );\n }\n /* istanbul ignore next */\n}\n","import {\n EXPECTED_COLOR,\n matcherHint,\n MatcherHintOptions,\n RECEIVED_COLOR,\n} from 'jest-matcher-utils';\nimport {\n ActualResponse,\n ErrorCode,\n makeResponse,\n OpenApi2Spec,\n OpenApi3Spec,\n OpenApiSpec,\n RawResponse,\n ValidationError,\n} from '@ehuelsmann/openapi-validator';\nimport { joinWithNewLines, stringify } from '../utils';\n\nexport default function (\n this: jest.MatcherContext,\n received: unknown,\n openApiSpec: OpenApiSpec,\n): jest.CustomMatcherResult {\n const actualResponse = makeResponse(received as RawResponse);\n\n const validationError = openApiSpec.validateResponse(actualResponse);\n const pass = !validationError;\n\n const matcherHintOptions: MatcherHintOptions = {\n comment:\n \"Matches 'received' to a response defined in your API spec, then validates 'received' against it\",\n ...(this.isNot !== undefined && { isNot: this.isNot }),\n ...(this.promise !== undefined && { promise: this.promise }),\n };\n const hint = matcherHint(\n 'toSatisfyApiSpec',\n undefined,\n '',\n matcherHintOptions,\n );\n const message = pass\n ? () =>\n getExpectReceivedNotToSatisfyApiSpecMsg(\n actualResponse,\n openApiSpec,\n hint,\n )\n : () =>\n getExpectReceivedToSatisfyApiSpecMsg(\n actualResponse,\n openApiSpec,\n validationError,\n hint,\n );\n\n return {\n pass,\n message,\n };\n}\n\nfunction getExpectReceivedToSatisfyApiSpecMsg(\n actualResponse: ActualResponse,\n openApiSpec: OpenApiSpec,\n validationError: ValidationError,\n hint: string,\n): string {\n const { status, req } = actualResponse;\n const { method, path: requestPath } = req;\n const unmatchedEndpoint = `${method} ${requestPath}`;\n\n if (validationError.code === ErrorCode.ServerNotFound) {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching servers`,\n `Servers found in API spec: ${EXPECTED_COLOR((openApiSpec as OpenApi3Spec).getServerUrls().join(', '))}`,\n );\n }\n\n if (validationError.code === ErrorCode.BasePathNotFound) {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has basePath ${EXPECTED_COLOR((openApiSpec as OpenApi2Spec).spec.basePath)}`,\n );\n }\n\n if (validationError.code === ErrorCode.PathNotFound) {\n // prettier-ignore\n const pathNotFoundErrorMessage = joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching path`,\n `Paths found in API spec: ${EXPECTED_COLOR(openApiSpec.paths().join(', '))}`,\n );\n\n if (\n 'didUserDefineBasePath' in openApiSpec &&\n openApiSpec.didUserDefineBasePath\n ) {\n // prettier-ignore\n return joinWithNewLines(\n pathNotFoundErrorMessage,\n `'${requestPath}' matches basePath \\`${openApiSpec.spec.basePath}\\` but no <basePath/endpointPath> combinations`,\n );\n }\n\n if (\n 'didUserDefineServers' in openApiSpec &&\n openApiSpec.didUserDefineServers\n ) {\n return joinWithNewLines(\n pathNotFoundErrorMessage,\n `'${requestPath}' matches servers ${stringify(\n openApiSpec.getMatchingServerUrls(requestPath),\n )} but no <server/endpointPath> combinations`,\n );\n }\n return pathNotFoundErrorMessage;\n }\n\n const path = openApiSpec.findOpenApiPathMatchingRequest(req);\n const endpoint = `${method} ${path}`;\n\n if (validationError.code === ErrorCode.MethodNotFound) {\n const expectedPathItem = openApiSpec.findExpectedPathItem(req);\n const expectedRequestOperations = Object.keys(expectedPathItem)\n .map((operation) => operation.toUpperCase())\n .join(', ');\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request method ${RECEIVED_COLOR(method)}, but your API spec has no ${RECEIVED_COLOR(method)} operation defined for path '${path}'`,\n `Request operations found for path '${path}' in API spec: ${EXPECTED_COLOR(expectedRequestOperations)}`,\n );\n }\n\n if (validationError.code === ErrorCode.StatusNotFound) {\n const expectedResponseOperation =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n openApiSpec.findExpectedResponseOperation(req)!;\n const expectedResponseStatuses = Object.keys(\n expectedResponseOperation.responses,\n ).join(', ');\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had status ${RECEIVED_COLOR(status)}, but your API spec has no ${RECEIVED_COLOR(status)} response defined for endpoint '${endpoint}'`,\n `Response statuses found for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(expectedResponseStatuses)}`,\n );\n }\n\n // validationError.code === ErrorCode.InvalidBody\n const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} did not satisfy it because: ${validationError}`,\n `${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,\n `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,\n );\n}\n\nfunction getExpectReceivedNotToSatisfyApiSpecMsg(\n actualResponse: ActualResponse,\n openApiSpec: OpenApiSpec,\n hint: string,\n): string {\n const { status, req } = actualResponse;\n const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);\n const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(\n req,\n )}`;\n\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,\n `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,\n );\n}\n","import { inspect } from 'util';\n\nexport const stringify = (obj: unknown): string =>\n inspect(obj, { showHidden: false, depth: null });\n\nexport const joinWithNewLines = (...lines: string[]): string =>\n lines.join('\\n\\n');\n","import {\n EXPECTED_COLOR,\n matcherErrorMessage,\n matcherHint,\n MatcherHintOptions,\n printExpected,\n printWithType,\n RECEIVED_COLOR,\n} from 'jest-matcher-utils';\nimport type { OpenApiSpec, Schema, ValidationError } from '@ehuelsmann/openapi-validator';\nimport { joinWithNewLines, stringify } from '../utils';\n\nexport default function (\n this: jest.MatcherContext,\n received: unknown,\n schemaName: string,\n openApiSpec: OpenApiSpec,\n): jest.CustomMatcherResult {\n const matcherHintOptions: MatcherHintOptions = {\n comment:\n \"Matches 'received' to a schema defined in your API spec, then validates 'received' against it\",\n ...(this.isNot !== undefined && { isNot: this.isNot }),\n ...(this.promise !== undefined && { promise: this.promise }),\n };\n const hint = matcherHint(\n 'toSatisfySchemaInApiSpec',\n undefined,\n 'schemaName',\n matcherHintOptions,\n );\n\n const schema = openApiSpec.getSchemaObject(schemaName);\n if (!schema) {\n // alert users they are misusing this assertion\n throw new Error(\n matcherErrorMessage(\n hint,\n `${EXPECTED_COLOR('schemaName')} must match a schema in your API spec`,\n printWithType('schemaName', schemaName, printExpected),\n ),\n );\n }\n\n const validationError = openApiSpec.validateObject(received, schema);\n const pass = !validationError;\n\n const message = pass\n ? () =>\n getExpectReceivedNotToSatisfySchemaInApiSpecMsg(\n received,\n schemaName,\n schema,\n hint,\n )\n : () =>\n getExpectReceivedToSatisfySchemaInApiSpecMsg(\n received,\n schemaName,\n schema,\n validationError,\n hint,\n );\n\n return {\n pass,\n message,\n };\n}\n\nfunction getExpectReceivedToSatisfySchemaInApiSpecMsg(\n received: unknown,\n schemaName: string,\n schema: Schema,\n validationError: ValidationError,\n hint: string,\n): string {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy the '${schemaName}' schema defined in your API spec`,\n `${RECEIVED_COLOR('received')} did not satisfy it because: ${validationError}`,\n `${RECEIVED_COLOR('received')} was: ${RECEIVED_COLOR(stringify(received))}`,\n `The '${schemaName}' schema in API spec: ${EXPECTED_COLOR(stringify(schema))}`,\n );\n}\n\nfunction getExpectReceivedNotToSatisfySchemaInApiSpecMsg(\n received: unknown,\n schemaName: string,\n schema: Schema,\n hint: string,\n): string {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} not to satisfy the '${schemaName}' schema defined in your API spec`,\n `${RECEIVED_COLOR('received')} was: ${RECEIVED_COLOR(stringify(received))}`,\n `The '${schemaName}' schema in API spec: ${EXPEC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oBAAoB,UAAU;AAAA,IACpE,OAAG,2CAAe,UAAU,CAAC,gCAAgC,eAAe;AAAA,IAC5E,OAAG,2CAAe,UAAU,CAAC,aAAS,2CAAe,UAAU,QAAQ,CAAC,CAAC;AAAA,IACzE,QAAQ,UAAU,6BAAyB,2CAAe,UAAU,MAAM,CAAC,CAAC;AAAA,EAC9E;AACF;AAEA,SAAS,gDACP,UACA,YACA,QACA,MACQ;AAER,SAAO;AAAA,IACL;AAAA,IACA,gBAAY,2CAAe,UAAU,CAAC,wBAAwB,UAAU;AAAA,IACxE,OAAG,2CAAe,UAAU,CAAC,aAAS,2CAAe,UAAU,QAAQ,CAAC,CAAC;AAAA,IACzE,QAAQ,UAAU,6BAAyB,2CAAe,UAAU,MAAM,CAAC,CAAC;AAAA,EAC9E;AACF;;;AH7Ee,SAAR,cAAkB,kBAAoD;AAC3E,QAAM,kBAAc,uCAAY,gBAAgB;AAEhD,QAAM,eAAqC;AAAA,IACzC,iBAAiB,UAAmB;AAClC,aAAO,yBAAiB,KAAK,MAAM,UAAU,WAAW;AAAA,IAC1D;AAAA,IACA,yBAAyB,UAAmB,YAAoB;AAC9D,aAAO,iCAAyB;AAAA,QAC9B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAc,OAAoC;AAGxD,MAAI,eAAe,QAAW;AAC5B,eAAW,OAAO,YAAY;AAAA,EAChC,OAAO;AAEL,YAAQ;AAAA,MACN;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,MACF,EAAE,KAAK,IAAI;AAAA,IACb;AAAA,EACF;AAEF;","names":["import_openapi_validator","import_jest_matcher_utils"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { makeApiSpec } from "@ehuelsmann/openapi-validator";
|
|
3
|
+
|
|
4
|
+
// src/matchers/toSatisfyApiSpec.ts
|
|
5
|
+
import {
|
|
6
|
+
EXPECTED_COLOR,
|
|
7
|
+
matcherHint,
|
|
8
|
+
RECEIVED_COLOR
|
|
9
|
+
} from "jest-matcher-utils";
|
|
10
|
+
import {
|
|
11
|
+
ErrorCode,
|
|
12
|
+
makeResponse
|
|
13
|
+
} from "@ehuelsmann/openapi-validator";
|
|
14
|
+
|
|
15
|
+
// src/utils.ts
|
|
16
|
+
import { inspect } from "util";
|
|
17
|
+
var stringify = (obj) => inspect(obj, { showHidden: false, depth: null });
|
|
18
|
+
var joinWithNewLines = (...lines) => lines.join("\n\n");
|
|
19
|
+
|
|
20
|
+
// src/matchers/toSatisfyApiSpec.ts
|
|
21
|
+
function toSatisfyApiSpec_default(received, openApiSpec) {
|
|
22
|
+
const actualResponse = makeResponse(received);
|
|
23
|
+
const validationError = openApiSpec.validateResponse(actualResponse);
|
|
24
|
+
const pass = !validationError;
|
|
25
|
+
const matcherHintOptions = {
|
|
26
|
+
comment: "Matches 'received' to a response defined in your API spec, then validates 'received' against it",
|
|
27
|
+
...this.isNot !== void 0 && { isNot: this.isNot },
|
|
28
|
+
...this.promise !== void 0 && { promise: this.promise }
|
|
29
|
+
};
|
|
30
|
+
const hint = matcherHint(
|
|
31
|
+
"toSatisfyApiSpec",
|
|
32
|
+
void 0,
|
|
33
|
+
"",
|
|
34
|
+
matcherHintOptions
|
|
35
|
+
);
|
|
36
|
+
const message = pass ? () => getExpectReceivedNotToSatisfyApiSpecMsg(
|
|
37
|
+
actualResponse,
|
|
38
|
+
openApiSpec,
|
|
39
|
+
hint
|
|
40
|
+
) : () => getExpectReceivedToSatisfyApiSpecMsg(
|
|
41
|
+
actualResponse,
|
|
42
|
+
openApiSpec,
|
|
43
|
+
validationError,
|
|
44
|
+
hint
|
|
45
|
+
);
|
|
46
|
+
return {
|
|
47
|
+
pass,
|
|
48
|
+
message
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function getExpectReceivedToSatisfyApiSpecMsg(actualResponse, openApiSpec, validationError, hint) {
|
|
52
|
+
const { status, req } = actualResponse;
|
|
53
|
+
const { method, path: requestPath } = req;
|
|
54
|
+
const unmatchedEndpoint = `${method} ${requestPath}`;
|
|
55
|
+
if (validationError.code === ErrorCode.ServerNotFound) {
|
|
56
|
+
return joinWithNewLines(
|
|
57
|
+
hint,
|
|
58
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
59
|
+
`${RECEIVED_COLOR("received")} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching servers`,
|
|
60
|
+
`Servers found in API spec: ${EXPECTED_COLOR(openApiSpec.getServerUrls().join(", "))}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
if (validationError.code === ErrorCode.BasePathNotFound) {
|
|
64
|
+
return joinWithNewLines(
|
|
65
|
+
hint,
|
|
66
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
67
|
+
`${RECEIVED_COLOR("received")} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has basePath ${EXPECTED_COLOR(openApiSpec.spec.basePath)}`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (validationError.code === ErrorCode.PathNotFound) {
|
|
71
|
+
const pathNotFoundErrorMessage = joinWithNewLines(
|
|
72
|
+
hint,
|
|
73
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
|
|
74
|
+
`${RECEIVED_COLOR("received")} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching path`,
|
|
75
|
+
`Paths found in API spec: ${EXPECTED_COLOR(openApiSpec.paths().join(", "))}`
|
|
76
|
+
);
|
|
77
|
+
if ("didUserDefineBasePath" in openApiSpec && openApiSpec.didUserDefineBasePath) {
|
|
78
|
+
return joinWithNewLines(
|
|
79
|
+
pathNotFoundErrorMessage,
|
|
80
|
+
`'${requestPath}' matches basePath \`${openApiSpec.spec.basePath}\` but no <basePath/endpointPath> combinations`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if ("didUserDefineServers" in openApiSpec && openApiSpec.didUserDefineServers) {
|
|
84
|
+
return joinWithNewLines(
|
|
85
|
+
pathNotFoundErrorMessage,
|
|
86
|
+
`'${requestPath}' matches servers ${stringify(
|
|
87
|
+
openApiSpec.getMatchingServerUrls(requestPath)
|
|
88
|
+
)} but no <server/endpointPath> combinations`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return pathNotFoundErrorMessage;
|
|
92
|
+
}
|
|
93
|
+
const path = openApiSpec.findOpenApiPathMatchingRequest(req);
|
|
94
|
+
const endpoint = `${method} ${path}`;
|
|
95
|
+
if (validationError.code === ErrorCode.MethodNotFound) {
|
|
96
|
+
const expectedPathItem = openApiSpec.findExpectedPathItem(req);
|
|
97
|
+
const expectedRequestOperations = Object.keys(expectedPathItem).map((operation) => operation.toUpperCase()).join(", ");
|
|
98
|
+
return joinWithNewLines(
|
|
99
|
+
hint,
|
|
100
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
101
|
+
`${RECEIVED_COLOR("received")} had request method ${RECEIVED_COLOR(method)}, but your API spec has no ${RECEIVED_COLOR(method)} operation defined for path '${path}'`,
|
|
102
|
+
`Request operations found for path '${path}' in API spec: ${EXPECTED_COLOR(expectedRequestOperations)}`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (validationError.code === ErrorCode.StatusNotFound) {
|
|
106
|
+
const expectedResponseOperation = (
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
108
|
+
openApiSpec.findExpectedResponseOperation(req)
|
|
109
|
+
);
|
|
110
|
+
const expectedResponseStatuses = Object.keys(
|
|
111
|
+
expectedResponseOperation.responses
|
|
112
|
+
).join(", ");
|
|
113
|
+
return joinWithNewLines(
|
|
114
|
+
hint,
|
|
115
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
116
|
+
`${RECEIVED_COLOR("received")} had status ${RECEIVED_COLOR(status)}, but your API spec has no ${RECEIVED_COLOR(status)} response defined for endpoint '${endpoint}'`,
|
|
117
|
+
`Response statuses found for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(expectedResponseStatuses)}`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
121
|
+
return joinWithNewLines(
|
|
122
|
+
hint,
|
|
123
|
+
`expected ${RECEIVED_COLOR("received")} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
124
|
+
`${RECEIVED_COLOR("received")} did not satisfy it because: ${validationError}`,
|
|
125
|
+
`${RECEIVED_COLOR("received")} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,
|
|
126
|
+
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
function getExpectReceivedNotToSatisfyApiSpecMsg(actualResponse, openApiSpec, hint) {
|
|
130
|
+
const { status, req } = actualResponse;
|
|
131
|
+
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
132
|
+
const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(
|
|
133
|
+
req
|
|
134
|
+
)}`;
|
|
135
|
+
return joinWithNewLines(
|
|
136
|
+
hint,
|
|
137
|
+
`expected ${RECEIVED_COLOR("received")} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
|
|
138
|
+
`${RECEIVED_COLOR("received")} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,
|
|
139
|
+
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/matchers/toSatisfySchemaInApiSpec.ts
|
|
144
|
+
import {
|
|
145
|
+
EXPECTED_COLOR as EXPECTED_COLOR2,
|
|
146
|
+
matcherErrorMessage,
|
|
147
|
+
matcherHint as matcherHint2,
|
|
148
|
+
printExpected,
|
|
149
|
+
printWithType,
|
|
150
|
+
RECEIVED_COLOR as RECEIVED_COLOR2
|
|
151
|
+
} from "jest-matcher-utils";
|
|
152
|
+
function toSatisfySchemaInApiSpec_default(received, schemaName, openApiSpec) {
|
|
153
|
+
const matcherHintOptions = {
|
|
154
|
+
comment: "Matches 'received' to a schema defined in your API spec, then validates 'received' against it",
|
|
155
|
+
...this.isNot !== void 0 && { isNot: this.isNot },
|
|
156
|
+
...this.promise !== void 0 && { promise: this.promise }
|
|
157
|
+
};
|
|
158
|
+
const hint = matcherHint2(
|
|
159
|
+
"toSatisfySchemaInApiSpec",
|
|
160
|
+
void 0,
|
|
161
|
+
"schemaName",
|
|
162
|
+
matcherHintOptions
|
|
163
|
+
);
|
|
164
|
+
const schema = openApiSpec.getSchemaObject(schemaName);
|
|
165
|
+
if (!schema) {
|
|
166
|
+
throw new Error(
|
|
167
|
+
matcherErrorMessage(
|
|
168
|
+
hint,
|
|
169
|
+
`${EXPECTED_COLOR2("schemaName")} must match a schema in your API spec`,
|
|
170
|
+
printWithType("schemaName", schemaName, printExpected)
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
const validationError = openApiSpec.validateObject(received, schema);
|
|
175
|
+
const pass = !validationError;
|
|
176
|
+
const message = pass ? () => getExpectReceivedNotToSatisfySchemaInApiSpecMsg(
|
|
177
|
+
received,
|
|
178
|
+
schemaName,
|
|
179
|
+
schema,
|
|
180
|
+
hint
|
|
181
|
+
) : () => getExpectReceivedToSatisfySchemaInApiSpecMsg(
|
|
182
|
+
received,
|
|
183
|
+
schemaName,
|
|
184
|
+
schema,
|
|
185
|
+
validationError,
|
|
186
|
+
hint
|
|
187
|
+
);
|
|
188
|
+
return {
|
|
189
|
+
pass,
|
|
190
|
+
message
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function getExpectReceivedToSatisfySchemaInApiSpecMsg(received, schemaName, schema, validationError, hint) {
|
|
194
|
+
return joinWithNewLines(
|
|
195
|
+
hint,
|
|
196
|
+
`expected ${RECEIVED_COLOR2("received")} to satisfy the '${schemaName}' schema defined in your API spec`,
|
|
197
|
+
`${RECEIVED_COLOR2("received")} did not satisfy it because: ${validationError}`,
|
|
198
|
+
`${RECEIVED_COLOR2("received")} was: ${RECEIVED_COLOR2(stringify(received))}`,
|
|
199
|
+
`The '${schemaName}' schema in API spec: ${EXPECTED_COLOR2(stringify(schema))}`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
function getExpectReceivedNotToSatisfySchemaInApiSpecMsg(received, schemaName, schema, hint) {
|
|
203
|
+
return joinWithNewLines(
|
|
204
|
+
hint,
|
|
205
|
+
`expected ${RECEIVED_COLOR2("received")} not to satisfy the '${schemaName}' schema defined in your API spec`,
|
|
206
|
+
`${RECEIVED_COLOR2("received")} was: ${RECEIVED_COLOR2(stringify(received))}`,
|
|
207
|
+
`The '${schemaName}' schema in API spec: ${EXPECTED_COLOR2(stringify(schema))}`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/index.ts
|
|
212
|
+
function index_default(filepathOrObject) {
|
|
213
|
+
const openApiSpec = makeApiSpec(filepathOrObject);
|
|
214
|
+
const jestMatchers = {
|
|
215
|
+
toSatisfyApiSpec(received) {
|
|
216
|
+
return toSatisfyApiSpec_default.call(this, received, openApiSpec);
|
|
217
|
+
},
|
|
218
|
+
toSatisfySchemaInApiSpec(received, schemaName) {
|
|
219
|
+
return toSatisfySchemaInApiSpec_default.call(
|
|
220
|
+
this,
|
|
221
|
+
received,
|
|
222
|
+
schemaName,
|
|
223
|
+
openApiSpec
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const jestExpect = global.expect;
|
|
228
|
+
if (jestExpect !== void 0) {
|
|
229
|
+
jestExpect.extend(jestMatchers);
|
|
230
|
+
} else {
|
|
231
|
+
console.error(
|
|
232
|
+
[
|
|
233
|
+
"Unable to find Jest's global expect.",
|
|
234
|
+
"Please check you have configured jest-openapi correctly.",
|
|
235
|
+
"See https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#usage for help."
|
|
236
|
+
].join("\n")
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
export {
|
|
241
|
+
index_default as default
|
|
242
|
+
};
|
|
243
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/matchers/toSatisfyApiSpec.ts","../src/utils.ts","../src/matchers/toSatisfySchemaInApiSpec.ts"],"sourcesContent":["import { makeApiSpec, OpenAPISpecObject } from '@ehuelsmann/openapi-validator';\nimport toSatisfyApiSpec from './matchers/toSatisfyApiSpec';\nimport toSatisfySchemaInApiSpec from './matchers/toSatisfySchemaInApiSpec';\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace jest {\n interface Matchers<R> {\n /**\n * Check the HTTP response object satisfies a response defined in your OpenAPI spec.\n * [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-api-tests-validate-the-status-and-body-of-http-responses-against-your-openapi-spec)\n */\n toSatisfyApiSpec(): R;\n /**\n * Check the object satisfies a schema defined in your OpenAPI spec.\n * [See usage example](https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#in-unit-tests-validate-objects-against-schemas-defined-in-your-openapi-spec)\n */\n toSatisfySchemaInApiSpec(schemaName: string): R;\n }\n }\n}\n\nexport default function (filepathOrObject: string | OpenAPISpecObject): void {\n const openApiSpec = makeApiSpec(filepathOrObject);\n\n const jestMatchers: jest.ExpectExtendMap = {\n toSatisfyApiSpec(received: unknown) {\n return toSatisfyApiSpec.call(this, received, openApiSpec);\n },\n toSatisfySchemaInApiSpec(received: unknown, schemaName: string) {\n return toSatisfySchemaInApiSpec.call(\n this,\n received,\n schemaName,\n openApiSpec,\n );\n },\n };\n\n const jestExpect = (global as { expect?: jest.Expect }).expect;\n\n /* istanbul ignore next */\n if (jestExpect !== undefined) {\n jestExpect.extend(jestMatchers);\n } else {\n // eslint-disable-next-line no-console\n console.error(\n [\n \"Unable to find Jest's global expect.\",\n 'Please check you have configured jest-openapi correctly.',\n 'See https://github.com/openapi-library/OpenAPIValidators/tree/master/packages/jest-openapi#usage for help.',\n ].join('\\n'),\n );\n }\n /* istanbul ignore next */\n}\n","import {\n EXPECTED_COLOR,\n matcherHint,\n MatcherHintOptions,\n RECEIVED_COLOR,\n} from 'jest-matcher-utils';\nimport {\n ActualResponse,\n ErrorCode,\n makeResponse,\n OpenApi2Spec,\n OpenApi3Spec,\n OpenApiSpec,\n RawResponse,\n ValidationError,\n} from '@ehuelsmann/openapi-validator';\nimport { joinWithNewLines, stringify } from '../utils';\n\nexport default function (\n this: jest.MatcherContext,\n received: unknown,\n openApiSpec: OpenApiSpec,\n): jest.CustomMatcherResult {\n const actualResponse = makeResponse(received as RawResponse);\n\n const validationError = openApiSpec.validateResponse(actualResponse);\n const pass = !validationError;\n\n const matcherHintOptions: MatcherHintOptions = {\n comment:\n \"Matches 'received' to a response defined in your API spec, then validates 'received' against it\",\n ...(this.isNot !== undefined && { isNot: this.isNot }),\n ...(this.promise !== undefined && { promise: this.promise }),\n };\n const hint = matcherHint(\n 'toSatisfyApiSpec',\n undefined,\n '',\n matcherHintOptions,\n );\n const message = pass\n ? () =>\n getExpectReceivedNotToSatisfyApiSpecMsg(\n actualResponse,\n openApiSpec,\n hint,\n )\n : () =>\n getExpectReceivedToSatisfyApiSpecMsg(\n actualResponse,\n openApiSpec,\n validationError,\n hint,\n );\n\n return {\n pass,\n message,\n };\n}\n\nfunction getExpectReceivedToSatisfyApiSpecMsg(\n actualResponse: ActualResponse,\n openApiSpec: OpenApiSpec,\n validationError: ValidationError,\n hint: string,\n): string {\n const { status, req } = actualResponse;\n const { method, path: requestPath } = req;\n const unmatchedEndpoint = `${method} ${requestPath}`;\n\n if (validationError.code === ErrorCode.ServerNotFound) {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching servers`,\n `Servers found in API spec: ${EXPECTED_COLOR((openApiSpec as OpenApi3Spec).getServerUrls().join(', '))}`,\n );\n }\n\n if (validationError.code === ErrorCode.BasePathNotFound) {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has basePath ${EXPECTED_COLOR((openApiSpec as OpenApi2Spec).spec.basePath)}`,\n );\n }\n\n if (validationError.code === ErrorCode.PathNotFound) {\n // prettier-ignore\n const pathNotFoundErrorMessage = joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching path`,\n `Paths found in API spec: ${EXPECTED_COLOR(openApiSpec.paths().join(', '))}`,\n );\n\n if (\n 'didUserDefineBasePath' in openApiSpec &&\n openApiSpec.didUserDefineBasePath\n ) {\n // prettier-ignore\n return joinWithNewLines(\n pathNotFoundErrorMessage,\n `'${requestPath}' matches basePath \\`${openApiSpec.spec.basePath}\\` but no <basePath/endpointPath> combinations`,\n );\n }\n\n if (\n 'didUserDefineServers' in openApiSpec &&\n openApiSpec.didUserDefineServers\n ) {\n return joinWithNewLines(\n pathNotFoundErrorMessage,\n `'${requestPath}' matches servers ${stringify(\n openApiSpec.getMatchingServerUrls(requestPath),\n )} but no <server/endpointPath> combinations`,\n );\n }\n return pathNotFoundErrorMessage;\n }\n\n const path = openApiSpec.findOpenApiPathMatchingRequest(req);\n const endpoint = `${method} ${path}`;\n\n if (validationError.code === ErrorCode.MethodNotFound) {\n const expectedPathItem = openApiSpec.findExpectedPathItem(req);\n const expectedRequestOperations = Object.keys(expectedPathItem)\n .map((operation) => operation.toUpperCase())\n .join(', ');\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had request method ${RECEIVED_COLOR(method)}, but your API spec has no ${RECEIVED_COLOR(method)} operation defined for path '${path}'`,\n `Request operations found for path '${path}' in API spec: ${EXPECTED_COLOR(expectedRequestOperations)}`,\n );\n }\n\n if (validationError.code === ErrorCode.StatusNotFound) {\n const expectedResponseOperation =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n openApiSpec.findExpectedResponseOperation(req)!;\n const expectedResponseStatuses = Object.keys(\n expectedResponseOperation.responses,\n ).join(', ');\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} had status ${RECEIVED_COLOR(status)}, but your API spec has no ${RECEIVED_COLOR(status)} response defined for endpoint '${endpoint}'`,\n `Response statuses found for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(expectedResponseStatuses)}`,\n );\n }\n\n // validationError.code === ErrorCode.InvalidBody\n const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} did not satisfy it because: ${validationError}`,\n `${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,\n `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,\n );\n}\n\nfunction getExpectReceivedNotToSatisfyApiSpecMsg(\n actualResponse: ActualResponse,\n openApiSpec: OpenApiSpec,\n hint: string,\n): string {\n const { status, req } = actualResponse;\n const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);\n const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(\n req,\n )}`;\n\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,\n `${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,\n `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,\n );\n}\n","import { inspect } from 'util';\n\nexport const stringify = (obj: unknown): string =>\n inspect(obj, { showHidden: false, depth: null });\n\nexport const joinWithNewLines = (...lines: string[]): string =>\n lines.join('\\n\\n');\n","import {\n EXPECTED_COLOR,\n matcherErrorMessage,\n matcherHint,\n MatcherHintOptions,\n printExpected,\n printWithType,\n RECEIVED_COLOR,\n} from 'jest-matcher-utils';\nimport type { OpenApiSpec, Schema, ValidationError } from '@ehuelsmann/openapi-validator';\nimport { joinWithNewLines, stringify } from '../utils';\n\nexport default function (\n this: jest.MatcherContext,\n received: unknown,\n schemaName: string,\n openApiSpec: OpenApiSpec,\n): jest.CustomMatcherResult {\n const matcherHintOptions: MatcherHintOptions = {\n comment:\n \"Matches 'received' to a schema defined in your API spec, then validates 'received' against it\",\n ...(this.isNot !== undefined && { isNot: this.isNot }),\n ...(this.promise !== undefined && { promise: this.promise }),\n };\n const hint = matcherHint(\n 'toSatisfySchemaInApiSpec',\n undefined,\n 'schemaName',\n matcherHintOptions,\n );\n\n const schema = openApiSpec.getSchemaObject(schemaName);\n if (!schema) {\n // alert users they are misusing this assertion\n throw new Error(\n matcherErrorMessage(\n hint,\n `${EXPECTED_COLOR('schemaName')} must match a schema in your API spec`,\n printWithType('schemaName', schemaName, printExpected),\n ),\n );\n }\n\n const validationError = openApiSpec.validateObject(received, schema);\n const pass = !validationError;\n\n const message = pass\n ? () =>\n getExpectReceivedNotToSatisfySchemaInApiSpecMsg(\n received,\n schemaName,\n schema,\n hint,\n )\n : () =>\n getExpectReceivedToSatisfySchemaInApiSpecMsg(\n received,\n schemaName,\n schema,\n validationError,\n hint,\n );\n\n return {\n pass,\n message,\n };\n}\n\nfunction getExpectReceivedToSatisfySchemaInApiSpecMsg(\n received: unknown,\n schemaName: string,\n schema: Schema,\n validationError: ValidationError,\n hint: string,\n): string {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} to satisfy the '${schemaName}' schema defined in your API spec`,\n `${RECEIVED_COLOR('received')} did not satisfy it because: ${validationError}`,\n `${RECEIVED_COLOR('received')} was: ${RECEIVED_COLOR(stringify(received))}`,\n `The '${schemaName}' schema in API spec: ${EXPECTED_COLOR(stringify(schema))}`,\n );\n}\n\nfunction getExpectReceivedNotToSatisfySchemaInApiSpecMsg(\n received: unknown,\n schemaName: string,\n schema: Schema,\n hint: string,\n): string {\n // prettier-ignore\n return joinWithNewLines(\n hint,\n `expected ${RECEIVED_COLOR('received')} not to satisfy the '${schemaName}' schema defined in your API spec`,\n `${RECEIVED_COLOR('received')} was: ${RECEIVED_COLOR(stringify(received))}`,\n `The '${schemaName}' schema in API spec: ${EXPECTED_COLOR(stringify(schema))}`,\n );\n}\n"],"mappings":";AAAA,SAAS,mBAAsC;;;ACA/C;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,EACA;AAAA,OAMK;;;ACfP,SAAS,eAAe;AAEjB,IAAM,YAAY,CAAC,QACxB,QAAQ,KAAK,EAAE,YAAY,OAAO,OAAO,KAAK,CAAC;AAE1C,IAAM,mB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F;AAEA,SAAS,gDACP,UACA,YACA,QACA,MACQ;AAER,SAAO;AAAA,IACL;AAAA,IACA,YAAYC,gBAAe,UAAU,CAAC,wBAAwB,UAAU;AAAA,IACxE,GAAGA,gBAAe,UAAU,CAAC,SAASA,gBAAe,UAAU,QAAQ,CAAC,CAAC;AAAA,IACzE,QAAQ,UAAU,yBAAyBD,gBAAe,UAAU,MAAM,CAAC,CAAC;AAAA,EAC9E;AACF;;;AH7Ee,SAAR,cAAkB,kBAAoD;AAC3E,QAAM,cAAc,YAAY,gBAAgB;AAEhD,QAAM,eAAqC;AAAA,IACzC,iBAAiB,UAAmB;AAClC,aAAO,yBAAiB,KAAK,MAAM,UAAU,WAAW;AAAA,IAC1D;AAAA,IACA,yBAAyB,UAAmB,YAAoB;AAC9D,aAAO,iCAAyB;AAAA,QAC9B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAc,OAAoC;AAGxD,MAAI,eAAe,QAAW;AAC5B,eAAW,OAAO,YAAY;AAAA,EAChC,OAAO;AAEL,YAAQ;AAAA,MACN;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,MACF,EAAE,KAAK,IAAI;AAAA,IACb;AAAA,EACF;AAEF;","names":["EXPECTED_COLOR","matcherHint","RECEIVED_COLOR","matcherHint","EXPECTED_COLOR","RECEIVED_COLOR"]}
|
package/package.json
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ehuelsmann/jest-openapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Jest matchers for asserting that HTTP responses satisfy an OpenAPI spec",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
7
20
|
"scripts": {
|
|
8
21
|
"clean": "rimraf dist",
|
|
9
22
|
"clean:openapi-validator": "cd ../openapi-validator && yarn clean",
|
|
10
23
|
"format": "prettier --write ../../ --ignore-path ../../.prettierignore",
|
|
11
24
|
"lint": "tsc --noEmit --project tsconfig.eslint.json && eslint .",
|
|
12
25
|
"lint:fix": "yarn lint --fix",
|
|
13
|
-
"build": "
|
|
26
|
+
"build": "tsup",
|
|
14
27
|
"test": "jest",
|
|
15
28
|
"test:watch": "jest --watch",
|
|
16
29
|
"test:coverage": "yarn clean && yarn clean:openapi-validator && jest --coverage",
|
|
@@ -18,7 +31,7 @@
|
|
|
18
31
|
"test:ci": "yarn format && yarn lint && yarn test:coverage",
|
|
19
32
|
"prepack": "yarn build"
|
|
20
33
|
},
|
|
21
|
-
"repository": "https://github.com/
|
|
34
|
+
"repository": "https://github.com/ehuelsmann/OpenAPIValidators/tree/master/packages/jest-openapi",
|
|
22
35
|
"author": "rwalle61 <richard.lh.waller@gmail.com>",
|
|
23
36
|
"contributors": [
|
|
24
37
|
"Jonny Spruce <jspruce94@gmail.com>"
|
|
@@ -33,9 +46,9 @@
|
|
|
33
46
|
"assertions"
|
|
34
47
|
],
|
|
35
48
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/
|
|
49
|
+
"url": "https://github.com/ehuelsmann/OpenAPIValidators/issues"
|
|
37
50
|
},
|
|
38
|
-
"homepage": "https://github.com/
|
|
51
|
+
"homepage": "https://github.com/ehuelsmann/OpenAPIValidators/tree/master/packages/jest-openapi#readme",
|
|
39
52
|
"publishConfig": {
|
|
40
53
|
"access": "public"
|
|
41
54
|
},
|
|
@@ -44,7 +57,6 @@
|
|
|
44
57
|
],
|
|
45
58
|
"devDependencies": {
|
|
46
59
|
"@types/jest": "^30.0.0",
|
|
47
|
-
"@types/request-promise": "^4.1.48",
|
|
48
60
|
"@types/supertest": "^7.2.0",
|
|
49
61
|
"axios": "^1.15.0",
|
|
50
62
|
"eslint": "^7.11.0",
|
|
@@ -52,13 +64,13 @@
|
|
|
52
64
|
"express": "^5.2.1",
|
|
53
65
|
"fs-extra": "^9.0.1",
|
|
54
66
|
"jest": "^30.3.0",
|
|
55
|
-
"request-promise": "^4.2.6",
|
|
56
67
|
"supertest": "^7.2.2",
|
|
57
68
|
"ts-jest": "^29.0.0",
|
|
58
|
-
"ts-node": "^9.1.1"
|
|
69
|
+
"ts-node": "^9.1.1",
|
|
70
|
+
"tsup": "^8.5.1"
|
|
59
71
|
},
|
|
60
72
|
"dependencies": {
|
|
61
|
-
"
|
|
62
|
-
"
|
|
73
|
+
"@ehuelsmann/openapi-validator": "^0.16.0",
|
|
74
|
+
"jest-matcher-utils": "^29.0.0"
|
|
63
75
|
}
|
|
64
76
|
}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAI/E,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,IAAI,CAAC;QACb,UAAU,QAAQ,CAAC,CAAC;YAClB;;;eAGG;YACH,gBAAgB,IAAI,CAAC,CAAC;YACtB;;;eAGG;YACH,wBAAwB,CAAC,UAAU,EAAE,MAAM,GAAG,CAAC,CAAC;SACjD;KACF;CACF;AAED,MAAM,CAAC,OAAO,WAAW,gBAAgB,EAAE,MAAM,GAAG,iBAAiB,GAAG,IAAI,CAiC3E"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"toSatisfyApiSpec.d.ts","sourceRoot":"","sources":["../../src/matchers/toSatisfyApiSpec.ts"],"names":[],"mappings":"AAMA,OAAO,EAML,WAAW,EAGZ,MAAM,+BAA+B,CAAC;AAGvC,MAAM,CAAC,OAAO,WACZ,IAAI,EAAE,IAAI,CAAC,cAAc,EACzB,QAAQ,EAAE,OAAO,EACjB,WAAW,EAAE,WAAW,GACvB,IAAI,CAAC,mBAAmB,CAqC1B"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = default_1;
|
|
4
|
-
const jest_matcher_utils_1 = require("jest-matcher-utils");
|
|
5
|
-
const openapi_validator_1 = require("@ehuelsmann/openapi-validator");
|
|
6
|
-
const utils_1 = require("../utils");
|
|
7
|
-
function default_1(received, openApiSpec) {
|
|
8
|
-
const actualResponse = (0, openapi_validator_1.makeResponse)(received);
|
|
9
|
-
const validationError = openApiSpec.validateResponse(actualResponse);
|
|
10
|
-
const pass = !validationError;
|
|
11
|
-
const matcherHintOptions = {
|
|
12
|
-
comment: "Matches 'received' to a response defined in your API spec, then validates 'received' against it",
|
|
13
|
-
...(this.isNot !== undefined && { isNot: this.isNot }),
|
|
14
|
-
...(this.promise !== undefined && { promise: this.promise }),
|
|
15
|
-
};
|
|
16
|
-
const hint = (0, jest_matcher_utils_1.matcherHint)('toSatisfyApiSpec', undefined, '', matcherHintOptions);
|
|
17
|
-
const message = pass
|
|
18
|
-
? () => getExpectReceivedNotToSatisfyApiSpecMsg(actualResponse, openApiSpec, hint)
|
|
19
|
-
: () => getExpectReceivedToSatisfyApiSpecMsg(actualResponse, openApiSpec, validationError, hint);
|
|
20
|
-
return {
|
|
21
|
-
pass,
|
|
22
|
-
message,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function getExpectReceivedToSatisfyApiSpecMsg(actualResponse, openApiSpec, validationError, hint) {
|
|
26
|
-
const { status, req } = actualResponse;
|
|
27
|
-
const { method, path: requestPath } = req;
|
|
28
|
-
const unmatchedEndpoint = `${method} ${requestPath}`;
|
|
29
|
-
if (validationError.code === openapi_validator_1.ErrorCode.ServerNotFound) {
|
|
30
|
-
// prettier-ignore
|
|
31
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} had request path ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(requestPath)}, but your API spec has no matching servers`, `Servers found in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)(openApiSpec.getServerUrls().join(', '))}`);
|
|
32
|
-
}
|
|
33
|
-
if (validationError.code === openapi_validator_1.ErrorCode.BasePathNotFound) {
|
|
34
|
-
// prettier-ignore
|
|
35
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} had request path ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(requestPath)}, but your API spec has basePath ${(0, jest_matcher_utils_1.EXPECTED_COLOR)(openApiSpec.spec.basePath)}`);
|
|
36
|
-
}
|
|
37
|
-
if (validationError.code === openapi_validator_1.ErrorCode.PathNotFound) {
|
|
38
|
-
// prettier-ignore
|
|
39
|
-
const pathNotFoundErrorMessage = (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} had request path ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(requestPath)}, but your API spec has no matching path`, `Paths found in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)(openApiSpec.paths().join(', '))}`);
|
|
40
|
-
if ('didUserDefineBasePath' in openApiSpec &&
|
|
41
|
-
openApiSpec.didUserDefineBasePath) {
|
|
42
|
-
// prettier-ignore
|
|
43
|
-
return (0, utils_1.joinWithNewLines)(pathNotFoundErrorMessage, `'${requestPath}' matches basePath \`${openApiSpec.spec.basePath}\` but no <basePath/endpointPath> combinations`);
|
|
44
|
-
}
|
|
45
|
-
if ('didUserDefineServers' in openApiSpec &&
|
|
46
|
-
openApiSpec.didUserDefineServers) {
|
|
47
|
-
return (0, utils_1.joinWithNewLines)(pathNotFoundErrorMessage, `'${requestPath}' matches servers ${(0, utils_1.stringify)(openApiSpec.getMatchingServerUrls(requestPath))} but no <server/endpointPath> combinations`);
|
|
48
|
-
}
|
|
49
|
-
return pathNotFoundErrorMessage;
|
|
50
|
-
}
|
|
51
|
-
const path = openApiSpec.findOpenApiPathMatchingRequest(req);
|
|
52
|
-
const endpoint = `${method} ${path}`;
|
|
53
|
-
if (validationError.code === openapi_validator_1.ErrorCode.MethodNotFound) {
|
|
54
|
-
const expectedPathItem = openApiSpec.findExpectedPathItem(req);
|
|
55
|
-
const expectedRequestOperations = Object.keys(expectedPathItem)
|
|
56
|
-
.map((operation) => operation.toUpperCase())
|
|
57
|
-
.join(', ');
|
|
58
|
-
// prettier-ignore
|
|
59
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} had request method ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(method)}, but your API spec has no ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(method)} operation defined for path '${path}'`, `Request operations found for path '${path}' in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)(expectedRequestOperations)}`);
|
|
60
|
-
}
|
|
61
|
-
if (validationError.code === openapi_validator_1.ErrorCode.StatusNotFound) {
|
|
62
|
-
const expectedResponseOperation =
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
64
|
-
openApiSpec.findExpectedResponseOperation(req);
|
|
65
|
-
const expectedResponseStatuses = Object.keys(expectedResponseOperation.responses).join(', ');
|
|
66
|
-
// prettier-ignore
|
|
67
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} had status ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(status)}, but your API spec has no ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(status)} response defined for endpoint '${endpoint}'`, `Response statuses found for endpoint '${endpoint}' in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)(expectedResponseStatuses)}`);
|
|
68
|
-
}
|
|
69
|
-
// validationError.code === ErrorCode.InvalidBody
|
|
70
|
-
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
71
|
-
// prettier-ignore
|
|
72
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} did not satisfy it because: ${validationError}`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} contained: ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(actualResponse.toString())}`, `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)((0, utils_1.stringify)(responseDefinition))}`);
|
|
73
|
-
}
|
|
74
|
-
function getExpectReceivedNotToSatisfyApiSpecMsg(actualResponse, openApiSpec, hint) {
|
|
75
|
-
const { status, req } = actualResponse;
|
|
76
|
-
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
|
|
77
|
-
const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(req)}`;
|
|
78
|
-
// prettier-ignore
|
|
79
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} contained: ${(0, jest_matcher_utils_1.RECEIVED_COLOR)(actualResponse.toString())}`, `The '${status}' response defined for endpoint '${endpoint}' in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)((0, utils_1.stringify)(responseDefinition))}`);
|
|
80
|
-
}
|
|
81
|
-
//# sourceMappingURL=toSatisfyApiSpec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"toSatisfyApiSpec.js","sourceRoot":"","sources":["../../src/matchers/toSatisfyApiSpec.ts"],"names":[],"mappings":";;AAkBA,4BAyCC;AA3DD,2DAK4B;AAC5B,qEASuC;AACvC,oCAAuD;AAEvD,mBAEE,QAAiB,EACjB,WAAwB;IAExB,MAAM,cAAc,GAAG,IAAA,gCAAY,EAAC,QAAuB,CAAC,CAAC;IAE7D,MAAM,eAAe,GAAG,WAAW,CAAC,gBAAgB,CAAC,cAAc,CAAC,CAAC;IACrE,MAAM,IAAI,GAAG,CAAC,eAAe,CAAC;IAE9B,MAAM,kBAAkB,GAAuB;QAC7C,OAAO,EACL,iGAAiG;QACnG,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;QACtD,GAAG,CAAC,IAAI,CAAC,OAAO,KAAK,SAAS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;KAC7D,CAAC;IACF,MAAM,IAAI,GAAG,IAAA,gCAAW,EACtB,kBAAkB,EAClB,SAAS,EACT,EAAE,EACF,kBAAkB,CACnB,CAAC;IACF,MAAM,OAAO,GAAG,IAAI;QAClB,CAAC,CAAC,GAAG,EAAE,CACH,uCAAuC,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":["../../src/matchers/toSatisfySchemaInApiSpec.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAA2B,MAAM,+BAA+B,CAAC;AAG1F,MAAM,CAAC,OAAO,WACZ,IAAI,EAAE,IAAI,CAAC,cAAc,EACzB,QAAQ,EAAE,OAAO,EACjB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,GACvB,IAAI,CAAC,mBAAmB,CAkD1B"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = default_1;
|
|
4
|
-
const jest_matcher_utils_1 = require("jest-matcher-utils");
|
|
5
|
-
const utils_1 = require("../utils");
|
|
6
|
-
function default_1(received, schemaName, openApiSpec) {
|
|
7
|
-
const matcherHintOptions = {
|
|
8
|
-
comment: "Matches 'received' to a schema defined in your API spec, then validates 'received' against it",
|
|
9
|
-
...(this.isNot !== undefined && { isNot: this.isNot }),
|
|
10
|
-
...(this.promise !== undefined && { promise: this.promise }),
|
|
11
|
-
};
|
|
12
|
-
const hint = (0, jest_matcher_utils_1.matcherHint)('toSatisfySchemaInApiSpec', undefined, 'schemaName', matcherHintOptions);
|
|
13
|
-
const schema = openApiSpec.getSchemaObject(schemaName);
|
|
14
|
-
if (!schema) {
|
|
15
|
-
// alert users they are misusing this assertion
|
|
16
|
-
throw new Error((0, jest_matcher_utils_1.matcherErrorMessage)(hint, `${(0, jest_matcher_utils_1.EXPECTED_COLOR)('schemaName')} must match a schema in your API spec`, (0, jest_matcher_utils_1.printWithType)('schemaName', schemaName, jest_matcher_utils_1.printExpected)));
|
|
17
|
-
}
|
|
18
|
-
const validationError = openApiSpec.validateObject(received, schema);
|
|
19
|
-
const pass = !validationError;
|
|
20
|
-
const message = pass
|
|
21
|
-
? () => getExpectReceivedNotToSatisfySchemaInApiSpecMsg(received, schemaName, schema, hint)
|
|
22
|
-
: () => getExpectReceivedToSatisfySchemaInApiSpecMsg(received, schemaName, schema, validationError, hint);
|
|
23
|
-
return {
|
|
24
|
-
pass,
|
|
25
|
-
message,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function getExpectReceivedToSatisfySchemaInApiSpecMsg(received, schemaName, schema, validationError, hint) {
|
|
29
|
-
// prettier-ignore
|
|
30
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} to satisfy the '${schemaName}' schema defined in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} did not satisfy it because: ${validationError}`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} was: ${(0, jest_matcher_utils_1.RECEIVED_COLOR)((0, utils_1.stringify)(received))}`, `The '${schemaName}' schema in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)((0, utils_1.stringify)(schema))}`);
|
|
31
|
-
}
|
|
32
|
-
function getExpectReceivedNotToSatisfySchemaInApiSpecMsg(received, schemaName, schema, hint) {
|
|
33
|
-
// prettier-ignore
|
|
34
|
-
return (0, utils_1.joinWithNewLines)(hint, `expected ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} not to satisfy the '${schemaName}' schema defined in your API spec`, `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} was: ${(0, jest_matcher_utils_1.RECEIVED_COLOR)((0, utils_1.stringify)(received))}`, `The '${schemaName}' schema in API spec: ${(0, jest_matcher_utils_1.EXPECTED_COLOR)((0, utils_1.stringify)(schema))}`);
|
|
35
|
-
}
|
|
36
|
-
//# sourceMappingURL=toSatisfySchemaInApiSpec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"toSatisfySchemaInApiSpec.js","sourceRoot":"","sources":["../../src/matchers/toSatisfySchemaInApiSpec.ts"],"names":[],"mappings":";;AAYA,4BAuDC;AAnED,2DAQ4B;AAE5B,oCAAuD;AAEvD,mBAEE,QAAiB,EACjB,UAAkB,EAClB,WAAw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rB,IAAI,EACJ,YAAY,IAAA,mCAAc,EAAC,UAAU,CAAC,oBAAoB,UAAU,mCAAmC,EACvG,GAAG,IAAA,mCAAc,EAAC,UAAU,CAAC,gCAAgC,eAAe,EAAE,EAC9E,GAAG,IAAA,mCAAc,EAAC,UAAU,CAAC,SAAS,IAAA,mCAAc,EAAC,IAAA,iBAAS,EAAC,QAAQ,CAAC,CAAC,EAAE,EAC3E,QAAQ,UAAU,yBAAyB,IAAA,mCAAc,EAAC,IAAA,iBAAS,EAAC,MAAM,CAAC,CAAC,EAAE,CAC/E,CAAC;AACJ,CAAC;AAED,SAAS,+CAA+C,CACtD,QAAiB,EACjB,UAAkB,EAClB,MAAc,EACd,IAAY;IAEZ,kBAAkB;IAClB,OAAO,IAAA,wBAAgB,EACrB,IAAI,EACJ,YAAY,IAAA,mCAAc,EAAC,UAAU,CAAC,wBAAwB,UAAU,mCAAmC,EAC3G,GAAG,IAAA,mCAAc,EAAC,UAAU,CAAC,SAAS,IAAA,mCAAc,EAAC,IAAA,iBAAS,EAAC,QAAQ,CAAC,CAAC,EAAE,EAC3E,QAAQ,UAAU,yBAAyB,IAAA,mCAAc,EAAC,IAAA,iBAAS,EAAC,MAAM,CAAC,CAAC,EAAE,CAC/E,CAAC;AACJ,CAAC"}
|
package/dist/utils.d.ts
DELETED
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,GAAI,KAAK,OAAO,KAAG,MACS,CAAC;AAEnD,eAAO,MAAM,gBAAgB,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,MAClC,CAAC"}
|
package/dist/utils.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.joinWithNewLines = exports.stringify = void 0;
|
|
4
|
-
const util_1 = require("util");
|
|
5
|
-
const stringify = (obj) => (0, util_1.inspect)(obj, { showHidden: false, depth: null });
|
|
6
|
-
exports.stringify = stringify;
|
|
7
|
-
const joinWithNewLines = (...lines) => lines.join('\n\n');
|
|
8
|
-
exports.joinWithNewLines = joinWithNewLines;
|
|
9
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAExB,MAAM,SAAS,GAAG,CAAC,GAAY,EAAU,EAAE,CAChD,IAAA,cAAO,EAAC,GAAG,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AADtC,QAAA,SAAS,aAC6B;AAE5C,MAAM,gBAAgB,GAAG,CAAC,GAAG,KAAe,EAAU,EAAE,CAC7D,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AADR,QAAA,gBAAgB,oBACR"}
|