@jaypie/testkit 1.1.0 → 1.1.1
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 +1 -324
- package/dist/__tests__/constants.spec.d.ts +1 -0
- package/dist/__tests__/expressHandler-supertest.mock.spec.d.ts +1 -0
- package/dist/__tests__/index.spec.d.ts +1 -0
- package/dist/__tests__/jaypie.mock.spec.d.ts +1 -0
- package/dist/__tests__/jsonApiSchema.module.spec.d.ts +1 -0
- package/dist/__tests__/matchers.module.spec.d.ts +1 -0
- package/dist/__tests__/mockLog.module.spec.d.ts +1 -0
- package/dist/__tests__/sqsTestRecords.function.spec.d.ts +1 -0
- package/dist/__tests__/toThrowError.matcher.spec.d.ts +1 -0
- package/dist/__tests__/toThrowJaypieError.matcher.spec.d.ts +1 -0
- package/dist/constants.d.ts +19 -0
- package/{src/index.js → dist/index.d.ts} +4 -15
- package/dist/index.js +467 -0
- package/dist/index.js.map +1 -0
- package/dist/jaypie.mock.d.ts +104 -0
- package/dist/jsonApiSchema.module.d.ts +61 -0
- package/dist/matchers/toBeCalledAboveTrace.matcher.d.ts +3 -0
- package/dist/matchers/toBeCalledWithInitialParams.matcher.d.ts +4 -0
- package/dist/matchers/toBeClass.matcher.d.ts +3 -0
- package/dist/matchers/toBeJaypieError.matcher.d.ts +3 -0
- package/dist/matchers/toMatch.matcher.d.ts +12 -0
- package/dist/matchers/toThrowError.matcher.d.ts +4 -0
- package/dist/matchers/toThrowJaypieError.matcher.d.ts +16 -0
- package/dist/matchers.module.d.ts +25 -0
- package/dist/mockLog.module.d.ts +5 -0
- package/dist/sqsTestRecords.function.d.ts +14 -0
- package/package.json +28 -23
- package/src/constants.js +0 -28
- package/src/jaypie.mock.js +0 -371
- package/src/jsonApiSchema.module.js +0 -39
- package/src/matchers/toBeCalledAboveTrace.matcher.js +0 -38
- package/src/matchers/toBeCalledWithInitialParams.matcher.js +0 -44
- package/src/matchers/toBeClass.matcher.js +0 -34
- package/src/matchers/toBeJaypieError.matcher.js +0 -52
- package/src/matchers/toMatch.matcher.js +0 -76
- package/src/matchers/toThrowJaypieError.matcher.js +0 -111
- package/src/matchers.module.js +0 -58
- package/src/mockLog.module.js +0 -99
- package/src/sqsTestRecords.function.js +0 -42
package/README.md
CHANGED
|
@@ -12,330 +12,7 @@ npm install --save-dev @jaypie/testkit
|
|
|
12
12
|
|
|
13
13
|
### Example
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
The testkit provides a complete mock for Jaypie including:
|
|
18
|
-
|
|
19
|
-
* Log spying (`expect(log.warn).toHaveBeenCalled()`)
|
|
20
|
-
* Default responses for runtime-only functions (`connect`, `sendMessage`, `submitMetric`)
|
|
21
|
-
* No automatic error handling for handlers (which is good in production but obfuscates tests)
|
|
22
|
-
* Most non-utility functions are mocked to allow simple testing
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
vi.mock("jaypie", async () => vi.importActual("@jaypie/testkit/mock"));
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
#### Error Spying
|
|
29
|
-
|
|
30
|
-
```javascript
|
|
31
|
-
import { ConfigurationError } from "@jaypie/core";
|
|
32
|
-
|
|
33
|
-
vi.mock("jaypie", async () => vi.importActual("@jaypie/testkit/mock"));
|
|
34
|
-
|
|
35
|
-
test("ConfigurationError", () => {
|
|
36
|
-
try {
|
|
37
|
-
throw new ConfigurationError("Sorpresa!");
|
|
38
|
-
} catch (error) {
|
|
39
|
-
expect(error).toBeJaypieError();
|
|
40
|
-
expect(ConfigurationError).toHaveBeenCalled();
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
#### Log Spying
|
|
46
|
-
|
|
47
|
-
```javascript
|
|
48
|
-
import { log } from "jaypie";
|
|
49
|
-
|
|
50
|
-
vi.mock("jaypie", async () => vi.importActual("@jaypie/testkit/mock"));
|
|
51
|
-
|
|
52
|
-
afterEach(() => {
|
|
53
|
-
vi.clearAllMocks();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("log", () => {
|
|
57
|
-
expect(vi.isMockFunction(log.warn)).toBe(true);
|
|
58
|
-
expect(log.warn).not.toHaveBeenCalled();
|
|
59
|
-
log.warn("Danger");
|
|
60
|
-
expect(log.warn).toHaveBeenCalled();
|
|
61
|
-
expect(log.error).not.toHaveBeenCalled();
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
👺 Logging Conventions:
|
|
66
|
-
|
|
67
|
-
* Only use `log.trace` or `log.var` during "happy path"
|
|
68
|
-
* Use `log.debug` for edge cases
|
|
69
|
-
* Now you can add an "observability" test that will fail as soon as new code triggers an unexpected edge condition
|
|
70
|
-
|
|
71
|
-
```javascript
|
|
72
|
-
describe("Observability", () => {
|
|
73
|
-
it("Does not log above trace", async () => {
|
|
74
|
-
// Arrange
|
|
75
|
-
// TODO: "happy path" setup
|
|
76
|
-
// Act
|
|
77
|
-
await myNewFunction(); // TODO: add any "happy path" parameters
|
|
78
|
-
// Assert
|
|
79
|
-
expect(log).not.toBeCalledAboveTrace();
|
|
80
|
-
// or individually:
|
|
81
|
-
expect(log.debug).not.toHaveBeenCalled();
|
|
82
|
-
expect(log.info).not.toHaveBeenCalled();
|
|
83
|
-
expect(log.warn).not.toHaveBeenCalled();
|
|
84
|
-
expect(log.error).not.toHaveBeenCalled();
|
|
85
|
-
expect(log.fatal).not.toHaveBeenCalled();
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
> 👺 Follow the "arrange, act, assert" pattern
|
|
91
|
-
|
|
92
|
-
#### Test Matchers
|
|
93
|
-
|
|
94
|
-
testSetup.js
|
|
95
|
-
|
|
96
|
-
```javascript
|
|
97
|
-
import { matchers as jaypieMatchers } from "@jaypie/testkit";
|
|
98
|
-
import * as extendedMatchers from "jest-extended";
|
|
99
|
-
import { expect } from "vitest";
|
|
100
|
-
|
|
101
|
-
expect.extend(extendedMatchers);
|
|
102
|
-
expect.extend(jaypieMatchers);
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
test.spec.js
|
|
106
|
-
|
|
107
|
-
```javascript
|
|
108
|
-
import { ConfigurationError } from "@jaypie/core";
|
|
109
|
-
|
|
110
|
-
const error = new ConfigurationError();
|
|
111
|
-
const json = error.json();
|
|
112
|
-
expect(error).toBeJaypieError();
|
|
113
|
-
expect(json).toBeJaypieError();
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## 📖 Reference
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
import {
|
|
120
|
-
LOG,
|
|
121
|
-
jsonApiErrorSchema,
|
|
122
|
-
jsonApiSchema,
|
|
123
|
-
matchers,
|
|
124
|
-
mockLogFactory,
|
|
125
|
-
restoreLog,
|
|
126
|
-
spyLog,
|
|
127
|
-
} from '@jaypie/testkit'
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### `LOG`
|
|
131
|
-
|
|
132
|
-
`LOG` constant provided by `@jaypie/core` for convenience
|
|
133
|
-
|
|
134
|
-
```javascript
|
|
135
|
-
import { log } from "@jaypie/core";
|
|
136
|
-
import { LOG } from "@jaypie/testkit";
|
|
137
|
-
|
|
138
|
-
const libLogger = log.lib({ level: LOG.LEVEL.WARN, lib: "myLib" });
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### `jsonApiErrorSchema`
|
|
142
|
-
|
|
143
|
-
A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://jsonapi.org/) error schema. Powers the `toBeJaypieError` matcher (via `toMatchSchema`).
|
|
144
|
-
|
|
145
|
-
### `jsonApiSchema`
|
|
146
|
-
|
|
147
|
-
A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://jsonapi.org/) data schema.
|
|
148
|
-
|
|
149
|
-
### `matchers`
|
|
150
|
-
|
|
151
|
-
```javascript
|
|
152
|
-
export default {
|
|
153
|
-
toBeCalledAboveTrace,
|
|
154
|
-
toBeCalledWithInitialParams,
|
|
155
|
-
toBeClass,
|
|
156
|
-
toBeJaypieError,
|
|
157
|
-
toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
|
|
158
|
-
toMatchBase64,
|
|
159
|
-
toMatchJwt,
|
|
160
|
-
toMatchMongoId,
|
|
161
|
-
toMatchSchema: jsonSchemaMatchers.toMatchSchema,
|
|
162
|
-
toMatchSignedCookie,
|
|
163
|
-
toMatchUuid4,
|
|
164
|
-
toMatchUuid5,
|
|
165
|
-
toMatchUuid,
|
|
166
|
-
toThrowBadGatewayError,
|
|
167
|
-
toThrowBadRequestError,
|
|
168
|
-
toThrowConfigurationError,
|
|
169
|
-
toThrowForbiddenError,
|
|
170
|
-
toThrowGatewayTimeoutError,
|
|
171
|
-
toThrowInternalError,
|
|
172
|
-
toThrowJaypieError,
|
|
173
|
-
toThrowNotFoundError,
|
|
174
|
-
toThrowUnauthorizedError,
|
|
175
|
-
toThrowUnavailableError,
|
|
176
|
-
};
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
testSetup.js
|
|
180
|
-
|
|
181
|
-
```javascript
|
|
182
|
-
import { matchers as jaypieMatchers } from "@jaypie/testkit";
|
|
183
|
-
import * as extendedMatchers from "jest-extended";
|
|
184
|
-
import { expect } from "vitest";
|
|
185
|
-
|
|
186
|
-
expect.extend(extendedMatchers);
|
|
187
|
-
expect.extend(jaypieMatchers);
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
#### `expect(subject).toBeCalledAboveTrace()`
|
|
191
|
-
|
|
192
|
-
```javascript
|
|
193
|
-
import { log } from "@jaypie/core";
|
|
194
|
-
|
|
195
|
-
log.trace("Hello, World!");
|
|
196
|
-
expect(log).not.toBeCalledAboveTrace();
|
|
197
|
-
|
|
198
|
-
log.warn("Look out, World!");
|
|
199
|
-
expect(log).toBeCalledAboveTrace();
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
#### `expect(subject).toBeJaypieError()`
|
|
203
|
-
|
|
204
|
-
Validates instance objects:
|
|
205
|
-
|
|
206
|
-
```javascript
|
|
207
|
-
try {
|
|
208
|
-
throw new Error("Sorpresa!");
|
|
209
|
-
} catch (error) {
|
|
210
|
-
expect(error).not.toBeJaypieError();
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
Validates plain old JSON:
|
|
215
|
-
|
|
216
|
-
```javascript
|
|
217
|
-
expect({ errors: [ { status, title, detail } ] }).toBeJaypieError();
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Jaypie errors, which are `ProjectErrors`, all have a `.json()` to convert
|
|
221
|
-
|
|
222
|
-
#### `expect(subject).toBeValidSchema()`
|
|
223
|
-
|
|
224
|
-
```javascript
|
|
225
|
-
import { jsonApiErrorSchema, jsonApiSchema } from "@jaypie/testkit";
|
|
226
|
-
|
|
227
|
-
expect(jsonApiErrorSchema).toBeValidSchema();
|
|
228
|
-
expect(jsonApiSchema).toBeValidSchema();
|
|
229
|
-
expect({ project: "mayhem" }).not.toBeValidSchema();
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
From `jest-json-schema` [toBeValidSchema.js](https://github.com/americanexpress/jest-json-schema/blob/main/matchers/toBeValidSchema.js) (not documented in README)
|
|
233
|
-
|
|
234
|
-
#### `expect(subject).toMatchSchema(schema)`
|
|
235
|
-
|
|
236
|
-
```javascript
|
|
237
|
-
import { jsonApiErrorSchema, jsonApiSchema } from "@jaypie/testkit";
|
|
238
|
-
import { ConfigurationError } from "@jaypie/core";
|
|
239
|
-
|
|
240
|
-
const error = new ConfigurationError();
|
|
241
|
-
const json = error.json();
|
|
242
|
-
expect(json).toMatchSchema(jsonApiErrorSchema);
|
|
243
|
-
expect(json).not.toMatchSchema(jsonApiSchema);
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
From `jest-json-schema`; see [README](https://github.com/americanexpress/jest-json-schema?tab=readme-ov-file#tomatchschemaschema)
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
#### `expect(subject).toMatch*()` Regular Expression Matchers
|
|
250
|
-
|
|
251
|
-
Note: these regular expressions matchers so not verify the value is value, only that it matches the pattern (it "looks like" something). For example, `expect("123e4567-e89b-12d3-a456-426614174000").toMatchUuid()` will pass because the string matches a UUID pattern, even though it is not a valid UUID.
|
|
252
|
-
|
|
253
|
-
* `toMatchBase64`
|
|
254
|
-
* `toMatchJwt`
|
|
255
|
-
* `toMatchMongoId`
|
|
256
|
-
* `toMatchSignedCookie`
|
|
257
|
-
* `toMatchUuid4`
|
|
258
|
-
* `toMatchUuid5`
|
|
259
|
-
* `toMatchUuid`
|
|
260
|
-
|
|
261
|
-
#### `expect(subject).toThrowJaypieError()`
|
|
262
|
-
|
|
263
|
-
```javascript
|
|
264
|
-
import { ConfigurationError } from "@jaypie/core";
|
|
265
|
-
|
|
266
|
-
const error = new ConfigurationError();
|
|
267
|
-
expect(() => {
|
|
268
|
-
throw error;
|
|
269
|
-
}).toThrowJaypieError();
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
Do not forget to `await expect` when passing `async` functions:
|
|
273
|
-
|
|
274
|
-
```javascript
|
|
275
|
-
import { ConfigurationError } from "@jaypie/core";
|
|
276
|
-
|
|
277
|
-
const error = new ConfigurationError();
|
|
278
|
-
await expect(async () => {
|
|
279
|
-
throw error;
|
|
280
|
-
}).toThrowJaypieError();
|
|
281
|
-
|
|
282
|
-
// Breaks and causes a false-positive because `expect` did not `await`
|
|
283
|
-
// expect(async () => {}).toThrowJaypieError();
|
|
284
|
-
// > Error: Expected function to throw a JaypieError, but it did not throw.
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
### `mockLogFactory()`
|
|
288
|
-
|
|
289
|
-
Creates a mock of the `log` provided by `@jaypie/core`.
|
|
290
|
-
|
|
291
|
-
```javascript
|
|
292
|
-
import { mockLogFactory } from "@jaypie/testkit";
|
|
293
|
-
|
|
294
|
-
const log = mockLogFactory();
|
|
295
|
-
log.warn("Danger");
|
|
296
|
-
expect(log.warn).toHaveBeenCalled();
|
|
297
|
-
expect(log.error).not.toHaveBeenCalled();
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
### `restoreLog(log)`
|
|
301
|
-
|
|
302
|
-
Restores the `log` provided by `@jaypie/core`, commonly performed `afterEach` with `spyLog` in `beforeEach`. See example with `spyLog`.
|
|
303
|
-
|
|
304
|
-
### `spyLog(log)`
|
|
305
|
-
|
|
306
|
-
Spies on the `log` provided by `@jaypie/core`, commonly performed `beforeEach` with `restoreLog` in `afterEach`. Not necessary when mocking the entire Jaypie module.
|
|
307
|
-
|
|
308
|
-
```javascript
|
|
309
|
-
import { restoreLog, spyLog } from "@jaypie/testkit";
|
|
310
|
-
import { log } from "@jaypie/core";
|
|
311
|
-
|
|
312
|
-
beforeEach(() => {
|
|
313
|
-
spyLog(log);
|
|
314
|
-
});
|
|
315
|
-
afterEach(() => {
|
|
316
|
-
restoreLog(log);
|
|
317
|
-
vi.clearAllMocks();
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
test("log", () => {
|
|
321
|
-
log.warn("Danger");
|
|
322
|
-
expect(log.warn).toHaveBeenCalled();
|
|
323
|
-
expect(log.error).not.toHaveBeenCalled();
|
|
324
|
-
});
|
|
325
|
-
```
|
|
326
|
-
|
|
327
|
-
### `sqsTestRecords(message, message, ...)` or `sqsTestRecords([...])`
|
|
328
|
-
|
|
329
|
-
Generates an event object for testing SQS Lambda functions with as many messages as provided. Note, test will accept more than ten messages, but AWS will only send ten at a time.
|
|
330
|
-
|
|
331
|
-
```javascript
|
|
332
|
-
import { sqsTestRecords } from "@jaypie/testkit";
|
|
333
|
-
|
|
334
|
-
const event = sqsTestRecords(
|
|
335
|
-
{ MessageId: "1", Body: "Hello, World!" },
|
|
336
|
-
{ MessageId: "2", Body: "Goodbye, World!" }
|
|
337
|
-
);
|
|
338
|
-
```
|
|
15
|
+
See [Jaypie](https://github.com/finlaysonstudio/jaypie) for usage.
|
|
339
16
|
|
|
340
17
|
## 🌠 Wishlist
|
|
341
18
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const LOG: {
|
|
2
|
+
readonly LEVEL: {
|
|
3
|
+
readonly ALL: "all";
|
|
4
|
+
readonly DEBUG: "debug";
|
|
5
|
+
readonly ERROR: "error";
|
|
6
|
+
readonly FATAL: "fatal";
|
|
7
|
+
readonly INFO: "info";
|
|
8
|
+
readonly SILENT: "silent";
|
|
9
|
+
readonly TRACE: "trace";
|
|
10
|
+
readonly WARN: "warn";
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare const RE_BASE64_PATTERN: RegExp;
|
|
14
|
+
export declare const RE_JWT_PATTERN: RegExp;
|
|
15
|
+
export declare const RE_MONGO_ID_PATTERN: RegExp;
|
|
16
|
+
export declare const RE_SIGNED_COOKIE_PATTERN: RegExp;
|
|
17
|
+
export declare const RE_UUID_4_PATTERN: RegExp;
|
|
18
|
+
export declare const RE_UUID_5_PATTERN: RegExp;
|
|
19
|
+
export declare const RE_UUID_PATTERN: RegExp;
|
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// Constants
|
|
4
|
-
//
|
|
5
|
-
|
|
6
1
|
export { LOG } from "./constants.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
export { jsonApiErrorSchema, jsonApiSchema } from "./jsonApiSchema.module.js";
|
|
14
|
-
export { default as matchers } from "./matchers.module.js";
|
|
15
|
-
export { mockLogFactory, restoreLog, spyLog } from "./mockLog.module.js";
|
|
16
|
-
export { default as sqsTestRecords } from "./sqsTestRecords.function.js";
|
|
2
|
+
export { jsonApiErrorSchema, jsonApiSchema } from "./jsonApiSchema.module";
|
|
3
|
+
export { default as matchers } from "./matchers.module";
|
|
4
|
+
export { mockLogFactory, restoreLog, spyLog } from "./mockLog.module";
|
|
5
|
+
export { default as sqsTestRecords } from "./sqsTestRecords.function";
|