@jaypie/testkit 1.0.18 → 1.0.20

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
@@ -117,6 +117,30 @@ A [JSON Schema](https://json-schema.org/) validator for the [JSON:API](https://j
117
117
 
118
118
  ### `matchers`
119
119
 
120
+ ```javascript
121
+ export default {
122
+ toBeCalledWithInitialParams,
123
+ toBeClass,
124
+ toBeJaypieError,
125
+ toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
126
+ toMatchBase64,
127
+ toMatchJwt,
128
+ toMatchMongoId,
129
+ toMatchSchema: jsonSchemaMatchers.toMatchSchema,
130
+ toMatchSignedCookie,
131
+ toMatchUuid4,
132
+ toMatchUuid5,
133
+ toMatchUuid,
134
+ toThrowBadRequestError,
135
+ toThrowConfigurationError,
136
+ toThrowForbiddenError,
137
+ toThrowInternalError,
138
+ toThrowJaypieError,
139
+ toThrowNotFoundError,
140
+ toThrowUnauthorizedError,
141
+ };
142
+ ```
143
+
120
144
  testSetup.js
121
145
 
122
146
  ```javascript
@@ -174,6 +198,45 @@ expect(json).not.toMatchSchema(jsonApiSchema);
174
198
 
175
199
  From `jest-json-schema`; see [README](https://github.com/americanexpress/jest-json-schema?tab=readme-ov-file#tomatchschemaschema)
176
200
 
201
+
202
+ #### `expect(subject).toMatch*()` Regular Expression Matchers
203
+
204
+ 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.
205
+
206
+ * `toMatchBase64`
207
+ * `toMatchJwt`
208
+ * `toMatchMongoId`
209
+ * `toMatchSignedCookie`
210
+ * `toMatchUuid4`
211
+ * `toMatchUuid5`
212
+ * `toMatchUuid`
213
+
214
+ #### `expect(subject).toThrowJaypieError()`
215
+
216
+ ```javascript
217
+ import { ConfigurationError } from "@jaypie/core";
218
+
219
+ const error = new ConfigurationError();
220
+ expect(() => {
221
+ throw error;
222
+ }).toThrowJaypieError();
223
+ ```
224
+
225
+ Do not forget to `await expect` when passing `async` functions:
226
+
227
+ ```javascript
228
+ import { ConfigurationError } from "@jaypie/core";
229
+
230
+ const error = new ConfigurationError();
231
+ await expect(async () => {
232
+ throw error;
233
+ }).toThrowJaypieError();
234
+
235
+ // Breaks and causes a false-positive because `expect` did not `await`
236
+ // expect(async () => {}).toThrowJaypieError();
237
+ // > Error: Expected function to throw a JaypieError, but it did not throw.
238
+ ```
239
+
177
240
  ### `mockLogFactory()`
178
241
 
179
242
  Creates a mock of the `log` provided by `@jaypie/core`.
@@ -234,7 +297,6 @@ const event = sqsTestRecords(
234
297
  * matcher toBeJaypieData
235
298
  * matcher toBeJaypieDataObject
236
299
  * matcher toBeJaypieDataArray
237
- * matcher toThrowJaypieError
238
300
  * ...@knowdev/jest
239
301
 
240
302
  ## 📝 Changelog
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/testkit",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,10 +8,18 @@
8
8
  "format": "npm run format:package && npm run format:lint",
9
9
  "format:lint": "eslint --fix .",
10
10
  "format:package": "sort-package-json ./package.json",
11
+ "init:commander": "hygen jaypie commander-init",
11
12
  "init:deploy": "hygen jaypie workflow-npm",
12
13
  "lint": "eslint .",
13
14
  "new": "hygen jaypie vite",
15
+ "new:command": "hygen jaypie command",
16
+ "new:constants": "hygen jaypie constants",
17
+ "new:hygen": "hygen jaypie hygen",
18
+ "new:index": "hygen jaypie index",
19
+ "new:model": "hygen jaypie model",
20
+ "new:test": "hygen jaypie vitest",
14
21
  "test": "vitest",
22
+ "test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
15
23
  "test:spec:index": "vitest run ./src/__tests__/index.spec.js",
16
24
  "test:spec:jsonApiSchema.module": "vitest run ./src/__tests__/jsonApiSchema.module.spec.js",
17
25
  "test:spec:matchers.module": "vitest run ./src/__tests__/matchers.module.spec.js",
@@ -19,20 +27,19 @@
19
27
  "test:spec:sqsTestRecords.function": "vitest run ./src/__tests__/sqsTestRecords.function.spec.js",
20
28
  "test:spec:toBeCalledWithInitialParams.matcher": "vitest run ./src/matchers/__tests__/toBeCalledWithInitialParams.matcher.spec.js",
21
29
  "test:spec:toBeClass.matcher": "vitest run ./src/matchers/__tests__/toBeClass.matcher.spec.js",
22
- "test:spec:toBeJaypieError.matcher": "vitest run ./src/matchers/__tests__/toBeJaypieError.matcher.spec.js"
30
+ "test:spec:toBeJaypieError.matcher": "vitest run ./src/matchers/__tests__/toBeJaypieError.matcher.spec.js",
31
+ "test:spec:toMatch.matcher": "vitest run ./src/matchers/__tests__/toMatch.matcher.spec.js",
32
+ "test:spec:toThrowJaypieError.matcher": "vitest run ./src/matchers/__tests__/toThrowJaypieError.matcher.spec.js"
23
33
  },
24
34
  "dependencies": {
25
- "@jaypie/core": "^1.0.23",
35
+ "@jaypie/core": "^1.0.33",
26
36
  "jest-json-schema": "^6.1.0",
27
37
  "lodash.isequal": "^4.5.0",
28
38
  "uuid": "^9.0.1"
29
39
  },
30
40
  "devDependencies": {
31
41
  "eslint": "^8.57.0",
32
- "eslint-config-prettier": "^9.1.0",
33
- "eslint-plugin-import": "^2.29.1",
34
- "eslint-plugin-prettier": "^5.1.3",
35
- "eslint-plugin-vitest": "^0.3.26",
42
+ "eslint-config-jaypie": "^1.0.7",
36
43
  "hygen": "^6.2.11",
37
44
  "jest-extended": "^4.0.2",
38
45
  "prettier": "^3.2.5",
@@ -0,0 +1,28 @@
1
+ //
2
+ //
3
+ // Constants
4
+ //
5
+
6
+ export const LOG = {
7
+ LEVEL: {
8
+ ALL: "all",
9
+ DEBUG: "debug",
10
+ ERROR: "error",
11
+ FATAL: "fatal",
12
+ INFO: "info",
13
+ SILENT: "silent",
14
+ TRACE: "trace",
15
+ WARN: "warn",
16
+ },
17
+ };
18
+
19
+ export const RE_BASE64_PATTERN = /^[a-zA-Z0-9\\+\\/]+$/;
20
+ export const RE_JWT_PATTERN = /^([^.]+)\.([^.]+)\.([^.]+)$/;
21
+ export const RE_MONGO_ID_PATTERN = /^[a-f0-9]{24}$/i;
22
+ export const RE_SIGNED_COOKIE_PATTERN = /^s:([^.]+)\.([^.]+)$/;
23
+ export const RE_UUID_4_PATTERN =
24
+ /^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$/i;
25
+ export const RE_UUID_5_PATTERN =
26
+ /^[a-f0-9]{8}-?[a-f0-9]{4}-?5[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$/i;
27
+ export const RE_UUID_PATTERN =
28
+ /^[a-f0-9]{8}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$/i;
package/src/index.js CHANGED
@@ -3,19 +3,7 @@
3
3
  // Constants
4
4
  //
5
5
 
6
- export const LOG = {
7
- LEVEL: {
8
- ALL: "all",
9
- DEBUG: "debug",
10
- ERROR: "error",
11
- FATAL: "fatal",
12
- INFO: "info",
13
- SILENT: "silent",
14
- TRACE: "trace",
15
- WARN: "warn",
16
- },
17
- };
18
-
6
+ export { LOG } from "./constants.js";
19
7
  //
20
8
  //
21
9
  // Export
@@ -0,0 +1,76 @@
1
+ import {
2
+ RE_BASE64_PATTERN,
3
+ RE_JWT_PATTERN,
4
+ RE_MONGO_ID_PATTERN,
5
+ RE_SIGNED_COOKIE_PATTERN,
6
+ RE_UUID_4_PATTERN,
7
+ RE_UUID_5_PATTERN,
8
+ RE_UUID_PATTERN,
9
+ } from "../constants.js";
10
+
11
+ //
12
+ //
13
+ // Helper
14
+ //
15
+
16
+ function forSubjectToMatchPattern(
17
+ subject,
18
+ pattern,
19
+ { patternName = "pattern" } = {},
20
+ ) {
21
+ if (pattern.test(subject)) {
22
+ return {
23
+ message: () => `expected "${subject}" not to match ${patternName}`,
24
+ pass: true,
25
+ };
26
+ }
27
+ return {
28
+ message: () => `expected "${subject}" to match ${patternName}`,
29
+ pass: false,
30
+ };
31
+ }
32
+
33
+ //
34
+ //
35
+ // Main
36
+ //
37
+
38
+ export const toMatchBase64 = (subject) =>
39
+ forSubjectToMatchPattern(subject, RE_BASE64_PATTERN, {
40
+ patternName: "Base64",
41
+ });
42
+
43
+ export const toMatchJwt = (subject) =>
44
+ forSubjectToMatchPattern(subject, RE_JWT_PATTERN, {
45
+ patternName: "JWT",
46
+ });
47
+
48
+ export const toMatchMongoId = (subject) =>
49
+ forSubjectToMatchPattern(subject, RE_MONGO_ID_PATTERN, {
50
+ patternName: "MongoDbId",
51
+ });
52
+
53
+ export const toMatchSignedCookie = (subject) =>
54
+ forSubjectToMatchPattern(subject, RE_SIGNED_COOKIE_PATTERN, {
55
+ patternName: "Signed-Cookie",
56
+ });
57
+
58
+ export const toMatchUuid4 = (subject) =>
59
+ forSubjectToMatchPattern(subject, RE_UUID_4_PATTERN, {
60
+ patternName: "UUIDv4",
61
+ });
62
+
63
+ export const toMatchUuid5 = (subject) =>
64
+ forSubjectToMatchPattern(subject, RE_UUID_5_PATTERN, {
65
+ patternName: "UUIDv5",
66
+ });
67
+
68
+ /**
69
+ * Determines if subject matches a UUID pattern.
70
+ * Does _NOT_ check if the UUID is valid.
71
+ * @param {String} subject
72
+ */
73
+ export const toMatchUuid = (subject) =>
74
+ forSubjectToMatchPattern(subject, RE_UUID_PATTERN, {
75
+ patternName: "UUID",
76
+ });
@@ -0,0 +1,99 @@
1
+ import {
2
+ BadRequestError,
3
+ ConfigurationError,
4
+ ForbiddenError,
5
+ InternalError,
6
+ isJaypieError,
7
+ NotFoundError,
8
+ UnauthorizedError,
9
+ } from "@jaypie/core";
10
+
11
+ //
12
+ //
13
+ // Main
14
+ //
15
+
16
+ const toThrowJaypieError = async (received, expected) => {
17
+ const isAsync =
18
+ received.constructor.name === "AsyncFunction" ||
19
+ received.constructor.name === "Promise";
20
+
21
+ // If expected is a function, call it
22
+ if (typeof expected === "function") {
23
+ expected = expected();
24
+ }
25
+
26
+ try {
27
+ const result = received();
28
+
29
+ if (isAsync) {
30
+ await result;
31
+ }
32
+
33
+ // If no error is thrown, fail the test
34
+ return {
35
+ pass: false,
36
+ message: () =>
37
+ "Expected function to throw a JaypieError, but it did not throw.",
38
+ };
39
+ } catch (error) {
40
+ if (isJaypieError(error)) {
41
+ // If expected is also a JaypieError, check if the error matches
42
+ if (isJaypieError(expected)) {
43
+ // If the error does not match, fail the test
44
+ if (error._type !== expected._type) {
45
+ return {
46
+ pass: false,
47
+ message: () =>
48
+ `Expected function to throw "${expected._type}", but it threw "${error._type}"`,
49
+ };
50
+ }
51
+ }
52
+ return {
53
+ pass: true,
54
+ message: () =>
55
+ `Expected function not to throw a JaypieError, but it threw ${error}`,
56
+ };
57
+ }
58
+
59
+ return {
60
+ pass: false,
61
+ message: () =>
62
+ `Expected function to throw a JaypieError, but it threw ${error}`,
63
+ };
64
+ }
65
+ };
66
+
67
+ //
68
+ //
69
+ // Convenience Methods
70
+ //
71
+
72
+ const toThrowBadRequestError = (received) =>
73
+ toThrowJaypieError(received, BadRequestError);
74
+ const toThrowConfigurationError = (received) =>
75
+ toThrowJaypieError(received, ConfigurationError);
76
+ const toThrowForbiddenError = (received) =>
77
+ toThrowJaypieError(received, ForbiddenError);
78
+ const toThrowInternalError = (received) =>
79
+ toThrowJaypieError(received, InternalError);
80
+ const toThrowNotFoundError = (received) =>
81
+ toThrowJaypieError(received, NotFoundError);
82
+ const toThrowUnauthorizedError = (received) =>
83
+ toThrowJaypieError(received, UnauthorizedError);
84
+
85
+ //
86
+ //
87
+ // Export
88
+ //
89
+
90
+ export default toThrowJaypieError;
91
+
92
+ export {
93
+ toThrowBadRequestError,
94
+ toThrowConfigurationError,
95
+ toThrowForbiddenError,
96
+ toThrowInternalError,
97
+ toThrowNotFoundError,
98
+ toThrowUnauthorizedError,
99
+ };
@@ -3,6 +3,24 @@ import { matchers as jsonSchemaMatchers } from "jest-json-schema";
3
3
  import toBeCalledWithInitialParams from "./matchers/toBeCalledWithInitialParams.matcher.js";
4
4
  import toBeClass from "./matchers/toBeClass.matcher.js";
5
5
  import toBeJaypieError from "./matchers/toBeJaypieError.matcher.js";
6
+ import toThrowJaypieError, {
7
+ toThrowBadRequestError,
8
+ toThrowConfigurationError,
9
+ toThrowForbiddenError,
10
+ toThrowInternalError,
11
+ toThrowNotFoundError,
12
+ toThrowUnauthorizedError,
13
+ } from "./matchers/toThrowJaypieError.matcher.js";
14
+
15
+ import {
16
+ toMatchBase64,
17
+ toMatchJwt,
18
+ toMatchMongoId,
19
+ toMatchSignedCookie,
20
+ toMatchUuid4,
21
+ toMatchUuid5,
22
+ toMatchUuid,
23
+ } from "./matchers/toMatch.matcher.js";
6
24
 
7
25
  //
8
26
  //
@@ -14,5 +32,19 @@ export default {
14
32
  toBeClass,
15
33
  toBeJaypieError,
16
34
  toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
35
+ toMatchBase64,
36
+ toMatchJwt,
37
+ toMatchMongoId,
17
38
  toMatchSchema: jsonSchemaMatchers.toMatchSchema,
39
+ toMatchSignedCookie,
40
+ toMatchUuid4,
41
+ toMatchUuid5,
42
+ toMatchUuid,
43
+ toThrowBadRequestError,
44
+ toThrowConfigurationError,
45
+ toThrowForbiddenError,
46
+ toThrowInternalError,
47
+ toThrowJaypieError,
48
+ toThrowNotFoundError,
49
+ toThrowUnauthorizedError,
18
50
  };
@@ -7,6 +7,7 @@ export function mockLogFactory() {
7
7
  error: vi.fn(),
8
8
  fatal: vi.fn(),
9
9
  info: vi.fn(),
10
+ init: vi.fn(),
10
11
  lib: vi.fn(),
11
12
  tag: vi.fn(),
12
13
  trace: vi.fn(),
@@ -23,6 +24,7 @@ export function mockLogFactory() {
23
24
  mock.trace.var = mock.var;
24
25
  mock.warn.var = mock.var;
25
26
  // Have modules return correct objects
27
+ mock.init.mockReturnValue(null);
26
28
  mock.lib.mockReturnValue(mock);
27
29
  mock.with.mockReturnValue(mock);
28
30
 
@@ -32,6 +34,7 @@ export function mockLogFactory() {
32
34
  error: mock.error,
33
35
  fatal: mock.fatal,
34
36
  info: mock.info,
37
+ init: mock.init,
35
38
  lib: mock.lib,
36
39
  tag: mock.tag,
37
40
  trace: mock.trace,
@@ -55,6 +58,8 @@ const logMethodNames = [
55
58
  "error",
56
59
  "fatal",
57
60
  "info",
61
+ "init",
62
+ "lib",
58
63
  "tag",
59
64
  "trace",
60
65
  "untag",