@jaypie/testkit 0.1.2 → 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.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"author": "Finlayson Studio",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -15,12 +15,14 @@
|
|
|
15
15
|
"test:spec:index": "vitest run ./src/__tests__/index.spec.js",
|
|
16
16
|
"test:spec:jsonApiSchema.module": "vitest run ./src/__tests__/jsonApiSchema.module.spec.js",
|
|
17
17
|
"test:spec:matchers.module": "vitest run ./src/__tests__/matchers.module.spec.js",
|
|
18
|
-
"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"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"jest-json-schema": "^6.1.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
25
|
+
"@jaypie/core": "^0.3.7",
|
|
24
26
|
"eslint": "^8.57.0",
|
|
25
27
|
"eslint-config-prettier": "^9.1.0",
|
|
26
28
|
"eslint-plugin-import": "^2.29.1",
|
|
@@ -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;
|
package/src/matchers.module.js
CHANGED
|
@@ -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
|
};
|