@jaypie/testkit 1.0.24 → 1.0.26

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.
Files changed (2) hide show
  1. package/package.json +4 -1
  2. package/src/jaypie.mock.js +59 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/testkit",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "exports": {
@@ -26,6 +26,7 @@
26
26
  "test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
27
27
  "test:spec:index": "vitest run ./src/__tests__/index.spec.js",
28
28
  "test:spec:jaypie.mock": "vitest run ./src/__tests__/jaypie.mock.spec.js",
29
+ "test:spec:expressHandler.mock": "vitest run ./src/__tests__/expressHandler-supertest.mock.spec.js",
29
30
  "test:spec:jsonApiSchema.module": "vitest run ./src/__tests__/jsonApiSchema.module.spec.js",
30
31
  "test:spec:matchers.module": "vitest run ./src/__tests__/matchers.module.spec.js",
31
32
  "test:spec:mockLog.module": "vitest run ./src/__tests__/mockLog.module.spec.js",
@@ -46,10 +47,12 @@
46
47
  "devDependencies": {
47
48
  "eslint": "^8.57.0",
48
49
  "eslint-config-jaypie": "^1.0.7",
50
+ "express": "^4.19.2",
49
51
  "hygen": "^6.2.11",
50
52
  "jest-extended": "^4.0.2",
51
53
  "prettier": "^3.2.5",
52
54
  "sort-package-json": "^2.8.0",
55
+ "supertest": "^7.0.0",
53
56
  "vitest": "^1.4.0"
54
57
  }
55
58
  }
@@ -6,6 +6,7 @@ import {
6
6
  JAYPIE,
7
7
  log,
8
8
  UnavailableError,
9
+ UnhandledError,
9
10
  } from "@jaypie/core";
10
11
  import { beforeAll, vi } from "vitest";
11
12
 
@@ -189,49 +190,77 @@ export const expressHandler = vi.fn((handler, props = {}) => {
189
190
  }
190
191
  const jaypieFunction = jaypieHandler(handler, props);
191
192
  return async (req = {}, res = {}, ...extra) => {
192
- // For mocking, let's make sure res json, send, and status are functions
193
193
  const status = HTTP.CODE.OK;
194
- if (res && typeof res.status === "function") {
195
- res.status(200);
194
+ let response;
195
+ let responseError;
196
+ let supertestMode = false;
197
+ if (
198
+ res &&
199
+ typeof res.socket === "object" &&
200
+ res.constructor.name === "ServerResponse"
201
+ ) {
202
+ // Use the response object in supertest mode
203
+ supertestMode = true;
196
204
  }
197
- const response = jaypieFunction(req, res, ...extra);
198
- if (response) {
199
- if (typeof response === "object") {
200
- if (typeof response.json === "function") {
201
- if (res && typeof res.json === "function") {
202
- res.json(response.json());
203
- }
205
+ try {
206
+ response = await jaypieFunction(req, res, ...extra);
207
+ } catch (error) {
208
+ // In the mock context, if status is a function we are in a "supertest"
209
+ if (supertestMode) {
210
+ // In theory jaypieFunction has handled all errors
211
+ const errorStatus = error.status || HTTP.CODE.INTERNAL_SERVER_ERROR;
212
+ let errorResponse;
213
+ if (typeof error.json === "function") {
214
+ errorResponse = error.json();
204
215
  } else {
205
- if (res && typeof res.status === "function") {
216
+ // This should never happen
217
+ errorResponse = new UnhandledError().json();
218
+ }
219
+ res.status(errorStatus).json(errorResponse);
220
+ return;
221
+ } else {
222
+ // else, res.status is not a function, throw the error
223
+ throw error;
224
+ }
225
+ }
226
+ if (responseError) {
227
+ if (supertestMode) {
228
+ res.status(responseError.status || HTTP.CODE.INTERNAL_SERVER_ERROR);
229
+ } else {
230
+ throw responseError;
231
+ }
232
+ // response = response
233
+ }
234
+ if (supertestMode) {
235
+ if (response) {
236
+ // if (res && typeof res.status === "function") {
237
+ // res.status(200);
238
+ // }
239
+ if (typeof response === "object") {
240
+ if (typeof response.json === "function") {
241
+ res.json(response.json());
242
+ } else {
206
243
  res.status(status).json(response);
207
244
  }
208
- }
209
- } else if (typeof response === "string") {
210
- try {
211
- if (res && typeof res.status === "function") {
245
+ } else if (typeof response === "string") {
246
+ try {
212
247
  res.status(status).json(JSON.parse(response));
248
+ } catch (error) {
249
+ if (supertestMode) {
250
+ res.status(status).send(response);
251
+ }
213
252
  }
214
- } catch (error) {
215
- if (res && typeof res.status === "function") {
216
- res.status(status).send(response);
217
- }
218
- }
219
- } else if (response === true) {
220
- if (res && typeof res.status === "function") {
253
+ } else if (response === true) {
221
254
  res.status(HTTP.CODE.CREATED).send();
222
- }
223
- } else {
224
- if (res && typeof res.status === "function") {
255
+ } else {
225
256
  res.status(status).send(response);
226
257
  }
227
- }
228
- } else {
229
- // No response
230
- if (res && typeof res.status === "function") {
258
+ } else {
231
259
  res.status(HTTP.CODE.NO_CONTENT).send();
232
260
  }
261
+ } else {
262
+ return response;
233
263
  }
234
- return response;
235
264
  };
236
265
  });
237
266