@jaypie/testkit 1.0.26 → 1.0.28

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/README.md CHANGED
@@ -22,7 +22,7 @@ The testkit provides a complete mock for Jaypie including:
22
22
  * Most non-utility functions are mocked to allow simple testing
23
23
 
24
24
  ```javascript
25
- vi.mock("jaypie", vi.importActual("@jaypie/testkit"));
25
+ vi.mock("jaypie", async () => vi.importActual("@jaypie/testkit/mock"));
26
26
  ```
27
27
 
28
28
  #### Log Spying
@@ -30,7 +30,7 @@ vi.mock("jaypie", vi.importActual("@jaypie/testkit"));
30
30
  ```javascript
31
31
  import { log } from "jaypie";
32
32
 
33
- vi.mock("jaypie", vi.importActual("@jaypie/testkit"));
33
+ vi.mock("jaypie", async () => vi.importActual("@jaypie/testkit/mock"));
34
34
 
35
35
  afterEach(() => {
36
36
  vi.clearAllMocks();
@@ -59,6 +59,8 @@ describe("Observability", () => {
59
59
  // Act
60
60
  await myNewFunction(); // TODO: add any "happy path" parameters
61
61
  // Assert
62
+ expect(log).not.toBeCalledAboveTrace();
63
+ // or individually:
62
64
  expect(log.debug).not.toHaveBeenCalled();
63
65
  expect(log.info).not.toHaveBeenCalled();
64
66
  expect(log.warn).not.toHaveBeenCalled();
@@ -131,6 +133,7 @@ A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://j
131
133
 
132
134
  ```javascript
133
135
  export default {
136
+ toBeCalledAboveTrace,
134
137
  toBeCalledWithInitialParams,
135
138
  toBeClass,
136
139
  toBeJaypieError,
@@ -143,13 +146,16 @@ export default {
143
146
  toMatchUuid4,
144
147
  toMatchUuid5,
145
148
  toMatchUuid,
149
+ toThrowBadGatewayError,
146
150
  toThrowBadRequestError,
147
151
  toThrowConfigurationError,
148
152
  toThrowForbiddenError,
153
+ toThrowGatewayTimeoutError,
149
154
  toThrowInternalError,
150
155
  toThrowJaypieError,
151
156
  toThrowNotFoundError,
152
157
  toThrowUnauthorizedError,
158
+ toThrowUnavailableError,
153
159
  };
154
160
  ```
155
161
 
@@ -164,6 +170,18 @@ expect.extend(extendedMatchers);
164
170
  expect.extend(jaypieMatchers);
165
171
  ```
166
172
 
173
+ #### `expect(subject).toBeCalledAboveTrace()`
174
+
175
+ ```javascript
176
+ import { log } from "@jaypie/core";
177
+
178
+ log.trace("Hello, World!");
179
+ expect(log).not.toBeCalledAboveTrace();
180
+
181
+ log.warn("Look out, World!");
182
+ expect(log).toBeCalledAboveTrace();
183
+ ```
184
+
167
185
  #### `expect(subject).toBeJaypieError()`
168
186
 
169
187
  Validates instance objects:
@@ -315,6 +333,8 @@ const event = sqsTestRecords(
315
333
 
316
334
  | Date | Version | Summary |
317
335
  | ---------- | ------- | -------------- |
336
+ | 9/14/2024 | 1.0.28 | Matchers `toThrowBadGatewayError`, `toThrowGatewayTimeoutError`, `toThrowUnavailableError` |
337
+ | 9/13/2024 | 1.0.27 | Matcher `toBeCalledAboveTrace` |
318
338
  | 7/16/2024 | 1.0.21 | Export Jaypie mock as default |
319
339
  | 3/20/2024 | 1.0.2 | Export `LOG` |
320
340
  | 3/16/2024 | 1.0.0 | Artists ship |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/testkit",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "exports": {
@@ -24,13 +24,14 @@
24
24
  "new:test": "hygen jaypie vitest",
25
25
  "test": "vitest",
26
26
  "test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
27
+ "test:spec:expressHandler.mock": "vitest run ./src/__tests__/expressHandler-supertest.mock.spec.js",
27
28
  "test:spec:index": "vitest run ./src/__tests__/index.spec.js",
28
29
  "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",
30
30
  "test:spec:jsonApiSchema.module": "vitest run ./src/__tests__/jsonApiSchema.module.spec.js",
31
31
  "test:spec:matchers.module": "vitest run ./src/__tests__/matchers.module.spec.js",
32
32
  "test:spec:mockLog.module": "vitest run ./src/__tests__/mockLog.module.spec.js",
33
33
  "test:spec:sqsTestRecords.function": "vitest run ./src/__tests__/sqsTestRecords.function.spec.js",
34
+ "test:spec:toBeCalledAboveTrace.matcher": "vitest run ./src/matchers/__tests__/toBeCalledAboveTrace.matcher.spec.js",
34
35
  "test:spec:toBeCalledWithInitialParams.matcher": "vitest run ./src/matchers/__tests__/toBeCalledWithInitialParams.matcher.spec.js",
35
36
  "test:spec:toBeClass.matcher": "vitest run ./src/matchers/__tests__/toBeClass.matcher.spec.js",
36
37
  "test:spec:toBeJaypieError.matcher": "vitest run ./src/matchers/__tests__/toBeJaypieError.matcher.spec.js",
@@ -139,6 +139,12 @@ export const submitMetricSet = vi.fn(() => {
139
139
  // @jaypie/express
140
140
 
141
141
  export const expressHandler = vi.fn((handler, props = {}) => {
142
+ // If handler is an object and options is a function, swap them
143
+ if (typeof handler === "object" && typeof props === "function") {
144
+ const temp = handler;
145
+ handler = props;
146
+ props = temp;
147
+ }
142
148
  if (typeof handler !== "function") {
143
149
  throw new BadRequestError("handler must be a function");
144
150
  }
@@ -192,7 +198,6 @@ export const expressHandler = vi.fn((handler, props = {}) => {
192
198
  return async (req = {}, res = {}, ...extra) => {
193
199
  const status = HTTP.CODE.OK;
194
200
  let response;
195
- let responseError;
196
201
  let supertestMode = false;
197
202
  if (
198
203
  res &&
@@ -223,19 +228,9 @@ export const expressHandler = vi.fn((handler, props = {}) => {
223
228
  throw error;
224
229
  }
225
230
  }
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
231
  if (supertestMode) {
235
232
  if (response) {
236
- // if (res && typeof res.status === "function") {
237
- // res.status(200);
238
- // }
233
+ // res.status(200);
239
234
  if (typeof response === "object") {
240
235
  if (typeof response.json === "function") {
241
236
  res.json(response.json());
@@ -0,0 +1,47 @@
1
+ //
2
+ //
3
+ // Constants
4
+ //
5
+
6
+ //
7
+ //
8
+ // Helper Functions
9
+ //
10
+
11
+ //
12
+ //
13
+ // Main
14
+ //
15
+
16
+ const calledAboveTrace = (log) => {
17
+ // TODO: what if log is not an object?
18
+
19
+ try {
20
+ if (
21
+ log.debug.mock.calls.length > 0 ||
22
+ log.info.mock.calls.length > 0 ||
23
+ log.warn.mock.calls.length > 0 ||
24
+ log.error.mock.calls.length > 0 ||
25
+ log.fatal.mock.calls.length > 0
26
+ ) {
27
+ return {
28
+ message: () => `expected log not to have been called above trace`,
29
+ pass: true,
30
+ };
31
+ }
32
+ } catch (error) {
33
+ throw Error(`[calledAboveTrace] log is not a mock object`);
34
+ }
35
+
36
+ return {
37
+ message: () => `expected log not to have been called above trace`,
38
+ pass: false,
39
+ };
40
+ };
41
+
42
+ //
43
+ //
44
+ // Export
45
+ //
46
+
47
+ export default calledAboveTrace;
@@ -1,11 +1,6 @@
1
1
  import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
2
  import { jsonApiErrorSchema } from "../jsonApiSchema.module.js";
3
3
 
4
- //
5
- //
6
- // Constants
7
- //
8
-
9
4
  //
10
5
  //
11
6
  // Helper Functions
@@ -1,11 +1,14 @@
1
1
  import {
2
+ BadGatewayError,
2
3
  BadRequestError,
3
4
  ConfigurationError,
4
5
  ForbiddenError,
6
+ GatewayTimeoutError,
5
7
  InternalError,
6
8
  isJaypieError,
7
9
  NotFoundError,
8
10
  UnauthorizedError,
11
+ UnavailableError,
9
12
  } from "@jaypie/core";
10
13
 
11
14
  //
@@ -69,18 +72,24 @@ const toThrowJaypieError = async (received, expected) => {
69
72
  // Convenience Methods
70
73
  //
71
74
 
75
+ const toThrowBadGatewayError = (received) =>
76
+ toThrowJaypieError(received, BadGatewayError);
72
77
  const toThrowBadRequestError = (received) =>
73
78
  toThrowJaypieError(received, BadRequestError);
74
79
  const toThrowConfigurationError = (received) =>
75
80
  toThrowJaypieError(received, ConfigurationError);
76
81
  const toThrowForbiddenError = (received) =>
77
82
  toThrowJaypieError(received, ForbiddenError);
83
+ const toThrowGatewayTimeoutError = (received) =>
84
+ toThrowJaypieError(received, GatewayTimeoutError);
78
85
  const toThrowInternalError = (received) =>
79
86
  toThrowJaypieError(received, InternalError);
80
87
  const toThrowNotFoundError = (received) =>
81
88
  toThrowJaypieError(received, NotFoundError);
82
89
  const toThrowUnauthorizedError = (received) =>
83
90
  toThrowJaypieError(received, UnauthorizedError);
91
+ const toThrowUnavailableError = (received) =>
92
+ toThrowJaypieError(received, UnavailableError);
84
93
 
85
94
  //
86
95
  //
@@ -90,10 +99,13 @@ const toThrowUnauthorizedError = (received) =>
90
99
  export default toThrowJaypieError;
91
100
 
92
101
  export {
102
+ toThrowBadGatewayError,
93
103
  toThrowBadRequestError,
94
104
  toThrowConfigurationError,
95
105
  toThrowForbiddenError,
106
+ toThrowGatewayTimeoutError,
96
107
  toThrowInternalError,
97
108
  toThrowNotFoundError,
98
109
  toThrowUnauthorizedError,
110
+ toThrowUnavailableError,
99
111
  };
@@ -1,15 +1,19 @@
1
1
  import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
2
 
3
+ import toBeCalledAboveTrace from "./matchers/toBeCalledAboveTrace.matcher.js";
3
4
  import toBeCalledWithInitialParams from "./matchers/toBeCalledWithInitialParams.matcher.js";
4
5
  import toBeClass from "./matchers/toBeClass.matcher.js";
5
6
  import toBeJaypieError from "./matchers/toBeJaypieError.matcher.js";
6
7
  import toThrowJaypieError, {
8
+ toThrowBadGatewayError,
7
9
  toThrowBadRequestError,
8
10
  toThrowConfigurationError,
9
11
  toThrowForbiddenError,
12
+ toThrowGatewayTimeoutError,
10
13
  toThrowInternalError,
11
14
  toThrowNotFoundError,
12
15
  toThrowUnauthorizedError,
16
+ toThrowUnavailableError,
13
17
  } from "./matchers/toThrowJaypieError.matcher.js";
14
18
 
15
19
  import {
@@ -28,6 +32,7 @@ import {
28
32
  //
29
33
 
30
34
  export default {
35
+ toBeCalledAboveTrace,
31
36
  toBeCalledWithInitialParams,
32
37
  toBeClass,
33
38
  toBeJaypieError,
@@ -40,11 +45,14 @@ export default {
40
45
  toMatchUuid4,
41
46
  toMatchUuid5,
42
47
  toMatchUuid,
48
+ toThrowBadGatewayError,
43
49
  toThrowBadRequestError,
44
50
  toThrowConfigurationError,
45
51
  toThrowForbiddenError,
52
+ toThrowGatewayTimeoutError,
46
53
  toThrowInternalError,
47
54
  toThrowJaypieError,
48
55
  toThrowNotFoundError,
49
56
  toThrowUnauthorizedError,
57
+ toThrowUnavailableError,
50
58
  };