@jaypie/testkit 0.1.1 → 0.1.3

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": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -13,10 +13,16 @@
13
13
  "new": "hygen jaypie vite",
14
14
  "test": "vitest",
15
15
  "test:spec:index": "vitest run ./src/__tests__/index.spec.js",
16
+ "test:spec:jsonApiSchema.module": "vitest run ./src/__tests__/jsonApiSchema.module.spec.js",
16
17
  "test:spec:matchers.module": "vitest run ./src/__tests__/matchers.module.spec.js",
17
- "test:spec:mockLog.module": "vitest run ./src/__tests__/mockLog.module.spec.js"
18
+ "test:spec:mockLog.module": "vitest run ./src/__tests__/mockLog.module.spec.js",
19
+ "test:spec:toBeJaypieError.matcher": "vitest run ./src/matchers/__tests__/toBeJaypieError.matcher.spec.js"
20
+ },
21
+ "dependencies": {
22
+ "jest-json-schema": "^6.1.0"
18
23
  },
19
24
  "devDependencies": {
25
+ "@jaypie/core": "^0.3.7",
20
26
  "eslint": "^8.57.0",
21
27
  "eslint-config-prettier": "^9.1.0",
22
28
  "eslint-plugin-import": "^2.29.1",
@@ -27,8 +33,5 @@
27
33
  "prettier": "^3.2.5",
28
34
  "sort-package-json": "^2.8.0",
29
35
  "vitest": "^1.4.0"
30
- },
31
- "dependencies": {
32
- "jest-json-schema": "^6.1.0"
33
36
  }
34
37
  }
@@ -1,7 +1,14 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
 
3
3
  // Subject
4
- import { matchers, mockLogFactory, restoreLog, spyLog } from "../index.js";
4
+ import {
5
+ jsonApiErrorSchema,
6
+ jsonApiSchema,
7
+ matchers,
8
+ mockLogFactory,
9
+ restoreLog,
10
+ spyLog,
11
+ } from "../index.js";
5
12
 
6
13
  //
7
14
  //
@@ -15,6 +22,8 @@ describe("Index", () => {
15
22
  expect(spyLog).toBeFunction();
16
23
  });
17
24
  it("Exports matchers", () => {
25
+ expect(jsonApiErrorSchema).toBeObject();
26
+ expect(jsonApiSchema).toBeObject();
18
27
  expect(matchers).toBeObject();
19
28
  });
20
29
  });
@@ -0,0 +1,17 @@
1
+ // eslint-disable-next-line no-unused-vars
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
+
4
+ // Subject
5
+ import { jsonApiErrorSchema, jsonApiSchema } from "../jsonApiSchema.module.js";
6
+
7
+ //
8
+ //
9
+ // Run tests
10
+ //
11
+
12
+ describe("Json Api Schema Module", () => {
13
+ it("Exports objects", () => {
14
+ expect(jsonApiSchema).toBeObject();
15
+ expect(jsonApiErrorSchema).toBeObject();
16
+ });
17
+ });
package/src/index.js CHANGED
@@ -3,5 +3,6 @@
3
3
  // Export
4
4
  //
5
5
 
6
+ export { jsonApiErrorSchema, jsonApiSchema } from "./jsonApiSchema.module.js";
6
7
  export { default as matchers } from "./matchers.module.js";
7
8
  export { mockLogFactory, restoreLog, spyLog } from "./mockLog.module.js";
@@ -0,0 +1,39 @@
1
+ export const jsonApiErrorSchema = {
2
+ type: "object",
3
+ properties: {
4
+ errors: {
5
+ type: "array",
6
+ items: {
7
+ type: "object",
8
+ properties: {
9
+ status: { type: "number" },
10
+ title: { type: "string" },
11
+ detail: { type: "string" },
12
+ },
13
+ required: ["status", "title"],
14
+ },
15
+ minItems: 1,
16
+ },
17
+ },
18
+ required: ["errors"],
19
+ };
20
+
21
+ export const jsonApiSchema = {
22
+ type: "object",
23
+ properties: {
24
+ data: {
25
+ type: "object",
26
+ properties: {
27
+ id: { type: "string" },
28
+ type: { type: "string" },
29
+ attributes: { type: "object" },
30
+ links: { type: "object" },
31
+ meta: { type: "object" },
32
+ relationships: { type: "object" },
33
+ },
34
+ required: ["id", "type"],
35
+ },
36
+ meta: { type: "object" },
37
+ },
38
+ required: ["data"],
39
+ };
@@ -0,0 +1,100 @@
1
+ // eslint-disable-next-line no-unused-vars
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
+
4
+ import { ConfigurationError } from "@jaypie/core";
5
+
6
+ // Subject
7
+ import toBeJaypieError from "../toBeJaypieError.matcher.js";
8
+
9
+ //
10
+ //
11
+ // Mock constants
12
+ //
13
+
14
+ //
15
+ //
16
+ // Mock modules
17
+ //
18
+
19
+ //
20
+ //
21
+ // Mock environment
22
+ //
23
+
24
+ const DEFAULT_ENV = process.env;
25
+ beforeEach(() => {
26
+ process.env = { ...process.env };
27
+ });
28
+ afterEach(() => {
29
+ process.env = DEFAULT_ENV;
30
+ });
31
+
32
+ //
33
+ //
34
+ // Run tests
35
+ //
36
+
37
+ describe("To Be Jaypie Error Matcher", () => {
38
+ it("Is a function", () => {
39
+ expect(toBeJaypieError).toBeFunction();
40
+ });
41
+ describe("Error Conditions", () => {
42
+ it("Rejects instances of plain error objects", () => {
43
+ const error = new Error(
44
+ "If this is your first night at Fight Club, you have to fight",
45
+ );
46
+ const result = toBeJaypieError(error);
47
+ expect(result.message).toBeFunction();
48
+ expect(result.message()).toBeString();
49
+ expect(result.pass).toBeFalse();
50
+ });
51
+ it("Rejects if nothing passed", () => {
52
+ const result = toBeJaypieError();
53
+ expect(result.message).toBeFunction();
54
+ expect(result.message()).toBeString();
55
+ expect(result.pass).toBeFalse();
56
+ });
57
+ it("Rejects if non-object passed", () => {
58
+ const result = toBeJaypieError(12);
59
+ expect(result.message).toBeFunction();
60
+ expect(result.message()).toBeString();
61
+ expect(result.pass).toBeFalse();
62
+ });
63
+ it("Rejects if no errors array", () => {
64
+ const result = toBeJaypieError({});
65
+ expect(result.message).toBeFunction();
66
+ expect(result.message()).toBeString();
67
+ expect(result.pass).toBeFalse();
68
+ });
69
+ it("Rejects if errors array is empty", () => {
70
+ const result = toBeJaypieError({ errors: [] });
71
+ expect(result.message).toBeFunction();
72
+ expect(result.message()).toBeString();
73
+ expect(result.pass).toBeFalse();
74
+ });
75
+ it("Must match the entire json:api error schema", () => {
76
+ const result = toBeJaypieError({ errors: ["taco"] });
77
+ expect(result.message).toBeFunction();
78
+ expect(result.message()).toBeString();
79
+ expect(result.pass).toBeFalse();
80
+ });
81
+ });
82
+ describe("Happy Path", () => {
83
+ it("Matches instances of Jaypie error objects", () => {
84
+ const error = new ConfigurationError();
85
+ const result = toBeJaypieError(error);
86
+ expect(result.message).toBeFunction();
87
+ expect(result.message()).toBeString();
88
+ expect(result.pass).toBeTrue();
89
+ });
90
+ it("Matches plain old json errors", () => {
91
+ const error = new ConfigurationError(
92
+ "If this is your first night at Fight Club, you have to fight",
93
+ ).json();
94
+ const result = toBeJaypieError(error);
95
+ expect(result.message).toBeFunction();
96
+ expect(result.message()).toBeString();
97
+ expect(result.pass).toBeTrue();
98
+ });
99
+ });
100
+ });
@@ -0,0 +1,57 @@
1
+ import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
+ import { jsonApiErrorSchema } from "../jsonApiSchema.module.js";
3
+
4
+ //
5
+ //
6
+ // Constants
7
+ //
8
+
9
+ //
10
+ //
11
+ // Helper Functions
12
+ //
13
+
14
+ function isErrorObjectJaypieError(error) {
15
+ if (error.isProjectError) {
16
+ return {
17
+ message: () => `expected "${error}" not to be a Jaypie error`,
18
+ pass: true,
19
+ };
20
+ }
21
+ return {
22
+ message: () => `expected "${error}" to be a Jaypie error`,
23
+ pass: false,
24
+ };
25
+ }
26
+
27
+ //
28
+ //
29
+ // Main
30
+ //
31
+
32
+ const toBeJaypieError = (received) => {
33
+ // See if it is an instance of error:
34
+ if (received instanceof Error) {
35
+ return isErrorObjectJaypieError(received);
36
+ }
37
+
38
+ const result = jsonSchemaMatchers.toMatchSchema(received, jsonApiErrorSchema);
39
+ if (result.pass) {
40
+ return {
41
+ message: () => `expected ${received} not to be a Jaypie error`,
42
+ pass: true,
43
+ };
44
+ } else {
45
+ return {
46
+ message: () => `expected ${received} to be a Jaypie error`,
47
+ pass: false,
48
+ };
49
+ }
50
+ };
51
+
52
+ //
53
+ //
54
+ // Export
55
+ //
56
+
57
+ export default toBeJaypieError;
@@ -1,11 +1,14 @@
1
1
  import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
2
 
3
+ import toBeJaypieError from "./matchers/toBeJaypieError.matcher.js";
4
+
3
5
  //
4
6
  //
5
7
  // Export
6
8
  //
7
9
 
8
10
  export default {
11
+ toBeJaypieError,
9
12
  toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
10
13
  toMatchSchema: jsonSchemaMatchers.toMatchSchema,
11
14
  };