@jaypie/testkit 1.0.25 → 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.
- package/package.json +4 -1
- package/src/jaypie.mock.js +59 -29
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaypie/testkit",
|
|
3
|
-
"version": "1.0.
|
|
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
|
}
|
package/src/jaypie.mock.js
CHANGED
|
@@ -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
|
|
|
@@ -190,47 +191,76 @@ export const expressHandler = vi.fn((handler, props = {}) => {
|
|
|
190
191
|
const jaypieFunction = jaypieHandler(handler, props);
|
|
191
192
|
return async (req = {}, res = {}, ...extra) => {
|
|
192
193
|
const status = HTTP.CODE.OK;
|
|
193
|
-
|
|
194
|
-
|
|
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;
|
|
195
204
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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();
|
|
203
215
|
} else {
|
|
204
|
-
|
|
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 {
|
|
205
243
|
res.status(status).json(response);
|
|
206
244
|
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
try {
|
|
210
|
-
if (res && typeof res.status === "function") {
|
|
245
|
+
} else if (typeof response === "string") {
|
|
246
|
+
try {
|
|
211
247
|
res.status(status).json(JSON.parse(response));
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (supertestMode) {
|
|
250
|
+
res.status(status).send(response);
|
|
251
|
+
}
|
|
212
252
|
}
|
|
213
|
-
}
|
|
214
|
-
if (res && typeof res.status === "function") {
|
|
215
|
-
res.status(status).send(response);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} else if (response === true) {
|
|
219
|
-
if (res && typeof res.status === "function") {
|
|
253
|
+
} else if (response === true) {
|
|
220
254
|
res.status(HTTP.CODE.CREATED).send();
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
if (res && typeof res.status === "function") {
|
|
255
|
+
} else {
|
|
224
256
|
res.status(status).send(response);
|
|
225
257
|
}
|
|
226
|
-
}
|
|
227
|
-
} else {
|
|
228
|
-
// No response
|
|
229
|
-
if (res && typeof res.status === "function") {
|
|
258
|
+
} else {
|
|
230
259
|
res.status(HTTP.CODE.NO_CONTENT).send();
|
|
231
260
|
}
|
|
261
|
+
} else {
|
|
262
|
+
return response;
|
|
232
263
|
}
|
|
233
|
-
return response;
|
|
234
264
|
};
|
|
235
265
|
});
|
|
236
266
|
|