@efebia/jest-match-response 0.0.2 → 0.0.4

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 CHANGED
@@ -1,2 +1,9 @@
1
1
  export { buildToMatchJson, type Options } from "./toMatchJson";
2
2
  export { toMatchResponse } from "./toMatchResponse";
3
+ export declare const matchResponseAndReturn: (response: {
4
+ json: () => any | Promise<any>;
5
+ } & ({
6
+ status: number;
7
+ } | {
8
+ statusCode: number;
9
+ }), model: any) => Promise<any>;
package/lib/index.js CHANGED
@@ -1,7 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toMatchResponse = exports.buildToMatchJson = void 0;
3
+ exports.matchResponseAndReturn = exports.toMatchResponse = exports.buildToMatchJson = void 0;
4
4
  var toMatchJson_1 = require("./toMatchJson");
5
5
  Object.defineProperty(exports, "buildToMatchJson", { enumerable: true, get: function () { return toMatchJson_1.buildToMatchJson; } });
6
6
  var toMatchResponse_1 = require("./toMatchResponse");
7
7
  Object.defineProperty(exports, "toMatchResponse", { enumerable: true, get: function () { return toMatchResponse_1.toMatchResponse; } });
8
+ const matchResponseAndReturn = async (response, model) => {
9
+ const jsonValue = await response.json();
10
+ expect({ ...response, json() {
11
+ return jsonValue;
12
+ } })
13
+ .toMatchResponse(model);
14
+ return jsonValue;
15
+ };
16
+ exports.matchResponseAndReturn = matchResponseAndReturn;
@@ -1,9 +1,5 @@
1
- export declare const toMatchResponse: (received: any, statusCode: any, model: any) => Promise<void | {
2
- actual: any;
3
- message: () => string;
4
- name: string;
5
- pass: boolean;
6
- }>;
1
+ import type { MatcherFunction } from "expect";
2
+ export declare const toMatchResponse: MatcherFunction<[number, any]>;
7
3
  declare module "expect" {
8
4
  interface AsymmetricMatchers {
9
5
  toMatchResponse(statusCode: number, model: any): void;
@@ -3,23 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toMatchResponse = void 0;
4
4
  const globals_1 = require("@jest/globals");
5
5
  const toMatchResponse = async function (received, statusCode, model) {
6
- if (!(Object.hasOwn(received, 'json') && typeof received.json === 'function'))
6
+ if (typeof received !== 'object' || !received)
7
+ throw new Error('toMatchResponse: the received value needs to be of type object');
8
+ if (!('json' in received && typeof received.json === 'function'))
7
9
  throw new Error('toMatchResponse: the received value needs to have a .json method');
8
- if (!(Object.hasOwn(received, 'status') || Object.hasOwn(received, 'statusCode')))
10
+ if (!('status' in received || 'statusCode' in received))
9
11
  throw new Error('toMatchResponse: the received value needs to have a status or statusCode property');
10
- const status = received.status || received.statusCode;
12
+ const status = ('status' in received ? received.status : received.statusCode);
11
13
  const jsonResponse = await received.json();
12
14
  const statusCodePass = this.isNot ? status !== statusCode : status === statusCode;
15
+ const message = () => (this.utils.matcherHint('toMatchResponse', undefined, undefined, { comment: 'Match response equality', isNot: this.isNot, promise: this.promise }) +
16
+ '\n\n' +
17
+ `Expected Status Code: ${this.utils.printExpected(statusCode)}\n` +
18
+ `Received Status Code: ${this.utils.printReceived(status)}\n` +
19
+ `Received Body: ${this.utils.printReceived(jsonResponse)}`);
13
20
  if (!statusCodePass) {
14
- const message = () => (this.utils.matcherHint('toMatchResponse', undefined, undefined, { comment: 'Match response equality', isNot: this.isNot, promise: this.promise }) +
15
- '\n\n' +
16
- `Expected Status Code: ${this.utils.printExpected(statusCode)}\n` +
17
- `Received Status Code: ${this.utils.printReceived(status)}\n` +
18
- `Received Body: ${this.utils.printReceived(jsonResponse)}`);
19
21
  return { actual: status, message, name: 'toMatchResponse', pass: false };
20
22
  }
21
- if (this.isNot)
22
- return (0, globals_1.expect)(jsonResponse).not.toMatchJson(model);
23
- return (0, globals_1.expect)(jsonResponse).toMatchJson(model);
23
+ if (this.isNot) {
24
+ (0, globals_1.expect)(jsonResponse).not.toMatchJson(model);
25
+ return { pass: true, name: 'toMatchResponse', message };
26
+ }
27
+ (0, globals_1.expect)(jsonResponse).toMatchJson(model);
28
+ return { pass: true, name: 'toMatchResponse', message };
24
29
  };
25
30
  exports.toMatchResponse = toMatchResponse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@efebia/jest-match-response",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "license": "MIT",
5
5
  "devDependencies": {
6
6
  "@types/jest": "^29.2.5",