@jaypie/testkit 0.1.3 → 1.0.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/.vscode/settings.json +1 -0
- package/README.md +198 -2
- package/package.json +1 -1
- package/src/mockLog.module.js +3 -0
package/.vscode/settings.json
CHANGED
package/README.md
CHANGED
|
@@ -12,16 +12,212 @@ npm install --save-dev @jaypie/testkit
|
|
|
12
12
|
|
|
13
13
|
### Example
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
#### Log Spying
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { restoreLog, spyLog } from "@jaypie/testkit";
|
|
19
|
+
import { log } from "@jaypie/core";
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
spyLog(log);
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
restoreLog(log);
|
|
26
|
+
vi.clearAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("log", () => {
|
|
30
|
+
log.warn("Danger");
|
|
31
|
+
expect(log.warn).toHaveBeenCalled();
|
|
32
|
+
expect(log.error).not.toHaveBeenCalled();
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
👺 Logging Conventions:
|
|
37
|
+
|
|
38
|
+
* Only use `log.trace` or `log.var` during "happy path"
|
|
39
|
+
* Use `log.debug` for edge cases
|
|
40
|
+
* Now you can add an "observability" test that will fail as soon as new code triggers an unexpected edge condition
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
describe("Observability", () => {
|
|
44
|
+
it("Does not log above trace", async () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
// TODO: "happy path" setup
|
|
47
|
+
// Act
|
|
48
|
+
await myNewFunction(); // TODO: add any "happy path" parameters
|
|
49
|
+
// Assert
|
|
50
|
+
expect(log.debug).not.toHaveBeenCalled();
|
|
51
|
+
expect(log.info).not.toHaveBeenCalled();
|
|
52
|
+
expect(log.warn).not.toHaveBeenCalled();
|
|
53
|
+
expect(log.error).not.toHaveBeenCalled();
|
|
54
|
+
expect(log.fatal).not.toHaveBeenCalled();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
> 👺 Follow the "arrange, act, assert" pattern
|
|
60
|
+
|
|
61
|
+
#### Test Matchers
|
|
62
|
+
|
|
63
|
+
testSetup.js
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
import { matchers as jaypieMatchers } from "@jaypie/testkit";
|
|
67
|
+
import * as extendedMatchers from "jest-extended";
|
|
68
|
+
import { expect } from "vitest";
|
|
69
|
+
|
|
70
|
+
expect.extend(extendedMatchers);
|
|
71
|
+
expect.extend(jaypieMatchers);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
test.spec.js
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import { ConfigurationError } from "@jaypie/core";
|
|
78
|
+
|
|
79
|
+
const error = new ConfigurationError();
|
|
80
|
+
const json = error.json();
|
|
81
|
+
expect(error).toBeJaypieError();
|
|
82
|
+
expect(json).toBeJaypieError();
|
|
83
|
+
```
|
|
16
84
|
|
|
17
85
|
## 📖 Reference
|
|
18
86
|
|
|
19
|
-
|
|
87
|
+
```
|
|
88
|
+
import {
|
|
89
|
+
jsonApiErrorSchema,
|
|
90
|
+
jsonApiSchema,
|
|
91
|
+
matchers,
|
|
92
|
+
mockLogFactory,
|
|
93
|
+
restoreLog,
|
|
94
|
+
spyLog,
|
|
95
|
+
} from '@jaypie/testkit'
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `jsonApiErrorSchema`
|
|
99
|
+
|
|
100
|
+
A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://jsonapi.org/) error schema. Powers the `toBeJaypieError` matcher (via `toMatchSchema`).
|
|
101
|
+
|
|
102
|
+
### `jsonApiSchema`
|
|
103
|
+
|
|
104
|
+
A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://jsonapi.org/) data schema.
|
|
105
|
+
|
|
106
|
+
### `matchers`
|
|
107
|
+
|
|
108
|
+
testSetup.js
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
import { matchers as jaypieMatchers } from "@jaypie/testkit";
|
|
112
|
+
import * as extendedMatchers from "jest-extended";
|
|
113
|
+
import { expect } from "vitest";
|
|
114
|
+
|
|
115
|
+
expect.extend(extendedMatchers);
|
|
116
|
+
expect.extend(jaypieMatchers);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### `expect(subject).toBeJaypieError()`
|
|
120
|
+
|
|
121
|
+
Validates instance objects:
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
try {
|
|
125
|
+
throw new Error("Sorpresa!");
|
|
126
|
+
} catch (error) {
|
|
127
|
+
expect(error).not.toBeJaypieError();
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Validates plain old JSON:
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
expect({ errors: [ { status, title, detail } ] }).toBeJaypieError();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Jaypie errors, which are `ProjectErrors`, all have a `.json()` to convert
|
|
138
|
+
|
|
139
|
+
#### `expect(subject).toBeValidSchema()`
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
import { jsonApiErrorSchema, jsonApiSchema } from "@jaypie/testkit";
|
|
143
|
+
|
|
144
|
+
expect(jsonApiErrorSchema).toBeValidSchema();
|
|
145
|
+
expect(jsonApiSchema).toBeValidSchema();
|
|
146
|
+
expect({ project: "mayhem" }).not.toBeValidSchema();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
From `jest-json-schema` [toBeValidSchema.js](https://github.com/americanexpress/jest-json-schema/blob/main/matchers/toBeValidSchema.js) (not documented in README)
|
|
150
|
+
|
|
151
|
+
#### `expect(subject).toMatchSchema(schema)`
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
import { jsonApiErrorSchema, jsonApiSchema } from "@jaypie/testkit";
|
|
155
|
+
import { ConfigurationError } from "@jaypie/core";
|
|
156
|
+
|
|
157
|
+
const error = new ConfigurationError();
|
|
158
|
+
const json = error.json();
|
|
159
|
+
expect(json).toMatchSchema(jsonApiErrorSchema);
|
|
160
|
+
expect(json).not.toMatchSchema(jsonApiSchema);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
From `jest-json-schema`; see [README](https://github.com/americanexpress/jest-json-schema?tab=readme-ov-file#tomatchschemaschema)
|
|
164
|
+
|
|
165
|
+
### `mockLogFactory()`
|
|
166
|
+
|
|
167
|
+
Creates a mock of the `log` provided by `@jaypie/core`.
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
import { mockLogFactory } from "@jaypie/testkit";
|
|
171
|
+
|
|
172
|
+
const log = mockLogFactory();
|
|
173
|
+
log.warn("Danger");
|
|
174
|
+
expect(log.warn).toHaveBeenCalled();
|
|
175
|
+
expect(log.error).not.toHaveBeenCalled();
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### `restoreLog(log)`
|
|
179
|
+
|
|
180
|
+
Restores the `log` provided by `@jaypie/core`, commonly performed `afterEach` with `spyLog` in `beforeEach`. See example with `spyLog`.
|
|
181
|
+
|
|
182
|
+
### `spyLog(log)`
|
|
183
|
+
|
|
184
|
+
Spies on the `log` provided by `@jaypie/core`, commonly performed `beforeEach` with `restoreLog` in `afterEach`.
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
import { restoreLog, spyLog } from "@jaypie/testkit";
|
|
188
|
+
import { log } from "@jaypie/core";
|
|
189
|
+
|
|
190
|
+
beforeEach(() => {
|
|
191
|
+
spyLog(log);
|
|
192
|
+
});
|
|
193
|
+
afterEach(() => {
|
|
194
|
+
restoreLog(log);
|
|
195
|
+
vi.clearAllMocks();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("log", () => {
|
|
199
|
+
log.warn("Danger");
|
|
200
|
+
expect(log.warn).toHaveBeenCalled();
|
|
201
|
+
expect(log.error).not.toHaveBeenCalled();
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## 🌠 Wishlist
|
|
206
|
+
|
|
207
|
+
* matcher toBeHttpStatus
|
|
208
|
+
* matcher toBeJaypieAny
|
|
209
|
+
* matcher toBeJaypieData
|
|
210
|
+
* matcher toBeJaypieDataObject
|
|
211
|
+
* matcher toBeJaypieDataArray
|
|
212
|
+
* matcher toThrowJaypieError
|
|
213
|
+
* ...@knowdev/jest
|
|
20
214
|
|
|
21
215
|
## 📝 Changelog
|
|
22
216
|
|
|
23
217
|
| Date | Version | Summary |
|
|
24
218
|
| ---------- | ------- | -------------- |
|
|
219
|
+
| 3/16/2024 | 1.0.0 | Artists ship |
|
|
220
|
+
| 3/15/2024 | 0.1.0 | Initial deploy |
|
|
25
221
|
| 3/15/2024 | 0.0.1 | Initial commit |
|
|
26
222
|
|
|
27
223
|
## 📜 License
|
package/package.json
CHANGED
package/src/mockLog.module.js
CHANGED
|
@@ -7,6 +7,7 @@ export function mockLogFactory() {
|
|
|
7
7
|
error: vi.fn(),
|
|
8
8
|
fatal: vi.fn(),
|
|
9
9
|
info: vi.fn(),
|
|
10
|
+
lib: vi.fn(),
|
|
10
11
|
tag: vi.fn(),
|
|
11
12
|
trace: vi.fn(),
|
|
12
13
|
untag: vi.fn(),
|
|
@@ -22,6 +23,7 @@ export function mockLogFactory() {
|
|
|
22
23
|
mock.trace.var = mock.var;
|
|
23
24
|
mock.warn.var = mock.var;
|
|
24
25
|
// Have modules return correct objects
|
|
26
|
+
mock.lib.mockReturnValue(mock);
|
|
25
27
|
mock.with.mockReturnValue(mock);
|
|
26
28
|
|
|
27
29
|
// Create something in the shape of the module
|
|
@@ -30,6 +32,7 @@ export function mockLogFactory() {
|
|
|
30
32
|
error: mock.error,
|
|
31
33
|
fatal: mock.fatal,
|
|
32
34
|
info: mock.info,
|
|
35
|
+
lib: mock.lib,
|
|
33
36
|
tag: mock.tag,
|
|
34
37
|
trace: mock.trace,
|
|
35
38
|
untag: mock.untag,
|