@jaypie/testkit 1.0.26 → 1.0.27

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,
@@ -164,6 +167,18 @@ expect.extend(extendedMatchers);
164
167
  expect.extend(jaypieMatchers);
165
168
  ```
166
169
 
170
+ #### `expect(subject).toBeCalledAboveTrace()`
171
+
172
+ ```javascript
173
+ import { log } from "@jaypie/core";
174
+
175
+ log.trace("Hello, World!");
176
+ expect(log).not.toBeCalledAboveTrace();
177
+
178
+ log.warn("Look out, World!");
179
+ expect(log).toBeCalledAboveTrace();
180
+ ```
181
+
167
182
  #### `expect(subject).toBeJaypieError()`
168
183
 
169
184
  Validates instance objects:
@@ -315,6 +330,7 @@ const event = sqsTestRecords(
315
330
 
316
331
  | Date | Version | Summary |
317
332
  | ---------- | ------- | -------------- |
333
+ | 9/13/2024 | 1.0.27 | Matcher `toBeCalledAboveTrace` |
318
334
  | 7/16/2024 | 1.0.21 | Export Jaypie mock as default |
319
335
  | 3/20/2024 | 1.0.2 | Export `LOG` |
320
336
  | 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.27",
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,5 +1,6 @@
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";
@@ -28,6 +29,7 @@ import {
28
29
  //
29
30
 
30
31
  export default {
32
+ toBeCalledAboveTrace,
31
33
  toBeCalledWithInitialParams,
32
34
  toBeClass,
33
35
  toBeJaypieError,