@jaypie/testkit 1.0.17 → 1.0.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/testkit",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -19,10 +19,11 @@
19
19
  "test:spec:sqsTestRecords.function": "vitest run ./src/__tests__/sqsTestRecords.function.spec.js",
20
20
  "test:spec:toBeCalledWithInitialParams.matcher": "vitest run ./src/matchers/__tests__/toBeCalledWithInitialParams.matcher.spec.js",
21
21
  "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"
22
+ "test:spec:toBeJaypieError.matcher": "vitest run ./src/matchers/__tests__/toBeJaypieError.matcher.spec.js",
23
+ "test:spec:toThrowJaypieError.matcher": "vitest run ./src/matchers/__tests__/toThrowJaypieError.matcher.spec.js"
23
24
  },
24
25
  "dependencies": {
25
- "@jaypie/core": "^1.0.22",
26
+ "@jaypie/core": "^1.0.33",
26
27
  "jest-json-schema": "^6.1.0",
27
28
  "lodash.isequal": "^4.5.0",
28
29
  "uuid": "^9.0.1"
package/src/index.js CHANGED
@@ -1,3 +1,21 @@
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
+
1
19
  //
2
20
  //
3
21
  // Export
@@ -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,14 @@ 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";
6
14
 
7
15
  //
8
16
  //
@@ -15,4 +23,11 @@ export default {
15
23
  toBeJaypieError,
16
24
  toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
17
25
  toMatchSchema: jsonSchemaMatchers.toMatchSchema,
26
+ toThrowBadRequestError,
27
+ toThrowConfigurationError,
28
+ toThrowForbiddenError,
29
+ toThrowInternalError,
30
+ toThrowJaypieError,
31
+ toThrowNotFoundError,
32
+ toThrowUnauthorizedError,
18
33
  };
@@ -55,6 +55,7 @@ const logMethodNames = [
55
55
  "error",
56
56
  "fatal",
57
57
  "info",
58
+ "lib",
58
59
  "tag",
59
60
  "trace",
60
61
  "untag",