@efebia/jest-match-response 0.0.1
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/lib/index.d.ts +2 -0
- package/lib/index.js +7 -0
- package/lib/toMatchJson.d.ts +14 -0
- package/lib/toMatchJson.js +26 -0
- package/lib/toMatchResponse.d.ts +14 -0
- package/lib/toMatchResponse.js +22 -0
- package/package.json +29 -0
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toMatchResponse = exports.buildToMatchJson = void 0;
|
|
4
|
+
var toMatchJson_1 = require("./toMatchJson");
|
|
5
|
+
Object.defineProperty(exports, "buildToMatchJson", { enumerable: true, get: function () { return toMatchJson_1.buildToMatchJson; } });
|
|
6
|
+
var toMatchResponse_1 = require("./toMatchResponse");
|
|
7
|
+
Object.defineProperty(exports, "toMatchResponse", { enumerable: true, get: function () { return toMatchResponse_1.toMatchResponse; } });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Ajv from "ajv";
|
|
2
|
+
import type { MatcherFunction } from "expect";
|
|
3
|
+
export type Options = {
|
|
4
|
+
ajv: Ajv;
|
|
5
|
+
};
|
|
6
|
+
export declare const buildToMatchJson: (opts: Options) => MatcherFunction<[any]>;
|
|
7
|
+
declare module "expect" {
|
|
8
|
+
interface AsymmetricMatchers {
|
|
9
|
+
toMatchJson(model: any): void;
|
|
10
|
+
}
|
|
11
|
+
interface Matchers<R> {
|
|
12
|
+
toMatchJson(model: any): R;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildToMatchJson = void 0;
|
|
4
|
+
const buildToMatchJson = (opts) => {
|
|
5
|
+
const { ajv } = opts;
|
|
6
|
+
const toMatchJson = function (json, model) {
|
|
7
|
+
const validate = ajv.compile(model);
|
|
8
|
+
const validation = validate(json);
|
|
9
|
+
const pass = this.equals(validation, !this.isNot);
|
|
10
|
+
const showErrors = (!pass && !this.isNot) || (pass && this.isNot);
|
|
11
|
+
const message = () => this.utils.matcherHint("toMatchJson", undefined, undefined, {
|
|
12
|
+
comment: "Match JSON equality",
|
|
13
|
+
isNot: this.isNot,
|
|
14
|
+
promise: this.promise,
|
|
15
|
+
}) +
|
|
16
|
+
"\n\n" +
|
|
17
|
+
`Expected JSON Schema: ${this.utils.printExpected(model)}\n` +
|
|
18
|
+
`Received JSON: ${this.utils.printReceived(json)}` +
|
|
19
|
+
showErrors
|
|
20
|
+
? `\nValidation errors: ${this.utils.printReceived(JSON.stringify(validate.errors))}`
|
|
21
|
+
: "";
|
|
22
|
+
return { actual: json, message, name: "toMatchJson", pass };
|
|
23
|
+
};
|
|
24
|
+
return toMatchJson;
|
|
25
|
+
};
|
|
26
|
+
exports.buildToMatchJson = buildToMatchJson;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const toMatchResponse: (received: any, statusCode: any, model: any) => Promise<void | {
|
|
2
|
+
actual: number;
|
|
3
|
+
message: () => string;
|
|
4
|
+
name: string;
|
|
5
|
+
pass: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
declare module "expect" {
|
|
8
|
+
interface AsymmetricMatchers {
|
|
9
|
+
toMatchResponse(statusCode: number, model: any): void;
|
|
10
|
+
}
|
|
11
|
+
interface Matchers<R> {
|
|
12
|
+
toMatchResponse(statusCode: number, model: any): R;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toMatchResponse = void 0;
|
|
4
|
+
const globals_1 = require("@jest/globals");
|
|
5
|
+
const toMatchResponse = async function (received, statusCode, model) {
|
|
6
|
+
if (!(received instanceof Response))
|
|
7
|
+
throw new Error('toMatchResponse: the received value has to be a fetch Response object');
|
|
8
|
+
const jsonResponse = await received.json();
|
|
9
|
+
const statusCodePass = this.isNot ? received.status !== statusCode : received.status === statusCode;
|
|
10
|
+
if (!statusCodePass) {
|
|
11
|
+
const message = () => (this.utils.matcherHint('toMatchResponse', undefined, undefined, { comment: 'Match response equality', isNot: this.isNot, promise: this.promise }) +
|
|
12
|
+
'\n\n' +
|
|
13
|
+
`Expected Status Code: ${this.utils.printExpected(statusCode)}\n` +
|
|
14
|
+
`Received Status Code: ${this.utils.printReceived(received.status)}\n` +
|
|
15
|
+
`Received Body: ${this.utils.printReceived(jsonResponse)}`);
|
|
16
|
+
return { actual: received.status, message, name: 'toMatchResponse', pass: false };
|
|
17
|
+
}
|
|
18
|
+
if (this.isNot)
|
|
19
|
+
return (0, globals_1.expect)(jsonResponse).not.toMatchJson(model);
|
|
20
|
+
return (0, globals_1.expect)(jsonResponse).toMatchJson(model);
|
|
21
|
+
};
|
|
22
|
+
exports.toMatchResponse = toMatchResponse;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@efebia/jest-match-response",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"devDependencies": {
|
|
6
|
+
"@types/jest": "^29.2.5",
|
|
7
|
+
"@types/js-yaml": "^4.0.5",
|
|
8
|
+
"@types/node": "^18.11.18",
|
|
9
|
+
"jest": "^29.3.1",
|
|
10
|
+
"ts-jest": "^29.0.3",
|
|
11
|
+
"typescript": "^4.9.4",
|
|
12
|
+
"undici": "^5.24.0"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@jest/globals": ">=28.0.0",
|
|
16
|
+
"ajv": "^8.12.0",
|
|
17
|
+
"jest": ">=28.0.0"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"/lib"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18.0.0"
|
|
27
|
+
},
|
|
28
|
+
"main": "./lib"
|
|
29
|
+
}
|