@jaypie/testkit 1.0.21 → 1.0.23

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 CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@jaypie/testkit",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
+ "exports": {
7
+ ".": "./src/index.js",
8
+ "./mock": "./src/jaypie.mock.js"
9
+ },
6
10
  "main": "src/index.js",
7
11
  "scripts": {
8
12
  "format": "npm run format:package && npm run format:lint",
package/src/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import * as mockingJay from "./jaypie.mock.js";
2
-
3
1
  //
4
2
  //
5
3
  // Constants
@@ -16,5 +14,3 @@ export { jsonApiErrorSchema, jsonApiSchema } from "./jsonApiSchema.module.js";
16
14
  export { default as matchers } from "./matchers.module.js";
17
15
  export { mockLogFactory, restoreLog, spyLog } from "./mockLog.module.js";
18
16
  export { default as sqsTestRecords } from "./sqsTestRecords.function.js";
19
-
20
- export default mockingJay;
@@ -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 { BadRequestError, JAYPIE, log, UnavailableError } from "@jaypie/core";
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
- return jaypieHandler(handler, props);
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 jaypieHandler(handler, props);
242
+ return async (event, context, ...extra) => {
243
+ return jaypieHandler(handler, props)(event, context, ...extra);
244
+ };
192
245
  });
193
246
 
194
247
  // @jaypie/mongoose