@jaypie/testkit 1.0.21 → 1.0.22
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 +1 -1
- package/src/jaypie.mock.js +56 -3
package/package.json
CHANGED
package/src/jaypie.mock.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { getMessages as originalGetMessages } from "@jaypie/aws";
|
|
2
2
|
import { force, uuid as originalUuid } from "@jaypie/core";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
BadRequestError,
|
|
5
|
+
HTTP,
|
|
6
|
+
JAYPIE,
|
|
7
|
+
log,
|
|
8
|
+
UnavailableError,
|
|
9
|
+
} from "@jaypie/core";
|
|
4
10
|
import { beforeAll, vi } from "vitest";
|
|
5
11
|
|
|
6
12
|
import { spyLog } from "./mockLog.module.js";
|
|
@@ -181,14 +187,61 @@ export const expressHandler = vi.fn((handler, props = {}) => {
|
|
|
181
187
|
props.setup.push(localsSetup);
|
|
182
188
|
}
|
|
183
189
|
}
|
|
184
|
-
|
|
190
|
+
const jaypieFunction = jaypieHandler(handler, props);
|
|
191
|
+
return async (req, res = {}, ...extra) => {
|
|
192
|
+
// For mocking, let's make sure res json, send, and status are functions
|
|
193
|
+
const status = HTTP.CODE.OK;
|
|
194
|
+
if (res && typeof res.status === "function") {
|
|
195
|
+
res.status(200);
|
|
196
|
+
}
|
|
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
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
if (res && typeof res.status === "function") {
|
|
206
|
+
res.status(status).json(response);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} else if (typeof response === "string") {
|
|
210
|
+
try {
|
|
211
|
+
if (res && typeof res.status === "function") {
|
|
212
|
+
res.status(status).json(JSON.parse(response));
|
|
213
|
+
}
|
|
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") {
|
|
221
|
+
res.status(HTTP.CODE.CREATED).send();
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
if (res && typeof res.status === "function") {
|
|
225
|
+
res.status(status).send(response);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
// No response
|
|
230
|
+
if (res && typeof res.status === "function") {
|
|
231
|
+
res.status(HTTP.CODE.NO_CONTENT).send();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return response;
|
|
235
|
+
};
|
|
185
236
|
});
|
|
186
237
|
|
|
187
238
|
// @jaypie/lambda
|
|
188
239
|
|
|
189
240
|
// For testing, this is the same as the jaypieHandler
|
|
190
241
|
export const lambdaHandler = vi.fn((handler, props = {}) => {
|
|
191
|
-
return
|
|
242
|
+
return async (event, context, ...extra) => {
|
|
243
|
+
return jaypieHandler(handler, props)(event, context, ...extra);
|
|
244
|
+
};
|
|
192
245
|
});
|
|
193
246
|
|
|
194
247
|
// @jaypie/mongoose
|