@jaypie/testkit 1.1.0 → 1.1.2

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.
@@ -1,39 +0,0 @@
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
- };
@@ -1,38 +0,0 @@
1
- //
2
- //
3
- // Main
4
- //
5
-
6
- const calledAboveTrace = (log) => {
7
- // TODO: what if log is not an object?
8
-
9
- try {
10
- if (
11
- log.debug.mock.calls.length > 0 ||
12
- log.info.mock.calls.length > 0 ||
13
- log.warn.mock.calls.length > 0 ||
14
- log.error.mock.calls.length > 0 ||
15
- log.fatal.mock.calls.length > 0
16
- ) {
17
- return {
18
- message: () => `expected log not to have been called above trace`,
19
- pass: true,
20
- };
21
- }
22
- // eslint-disable-next-line no-unused-vars
23
- } catch (error) {
24
- throw Error(`[calledAboveTrace] log is not a mock object`);
25
- }
26
-
27
- return {
28
- message: () => `expected log not to have been called above trace`,
29
- pass: false,
30
- };
31
- };
32
-
33
- //
34
- //
35
- // Export
36
- //
37
-
38
- export default calledAboveTrace;
@@ -1,44 +0,0 @@
1
- import isEqual from "lodash.isequal";
2
-
3
- //
4
- //
5
- // Main
6
- //
7
-
8
- export default (received, ...passed) => {
9
- let pass;
10
-
11
- if (!received || typeof received !== "function" || !received.mock) {
12
- return {
13
- message: () =>
14
- `Expectation \`toBeCalledWithInitialParams\` expected a mock function`,
15
- pass: false,
16
- };
17
- }
18
-
19
- received.mock.calls.forEach((call) => {
20
- if (call.length >= passed.length) {
21
- let matching = true;
22
- for (let i = 0; i < passed.length && matching; i += 1) {
23
- if (!isEqual(passed[i], call[i])) matching = false;
24
- }
25
- pass = pass || matching;
26
- }
27
- });
28
-
29
- if (pass === undefined) pass = false;
30
-
31
- if (pass) {
32
- return {
33
- message: () =>
34
- `Expectation \`toBeCalledWithInitialParams\` expected call beginning with [${passed},...]`,
35
- pass: true,
36
- };
37
- } else {
38
- return {
39
- message: () =>
40
- `Expectation \`not.toBeCalledWithInitialParams\` did not expect call beginning with [${passed},...]`,
41
- pass: false,
42
- };
43
- }
44
- };
@@ -1,34 +0,0 @@
1
- //
2
- //
3
- // Main
4
- //
5
-
6
- const toBeClass = (received) => {
7
- let pass = false;
8
- if (typeof received === "function") {
9
- try {
10
- new received();
11
- pass = true;
12
- // eslint-disable-next-line no-unused-vars
13
- } catch (error) {
14
- pass = false;
15
- }
16
- }
17
- if (pass) {
18
- return {
19
- message: () => `expected ${received} not to be a class`,
20
- pass: true,
21
- };
22
- }
23
- return {
24
- message: () => `expected ${received} to be a class`,
25
- pass: false,
26
- };
27
- };
28
-
29
- //
30
- //
31
- // Export
32
- //
33
-
34
- export default toBeClass;
@@ -1,52 +0,0 @@
1
- import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
- import { jsonApiErrorSchema } from "../jsonApiSchema.module.js";
3
-
4
- //
5
- //
6
- // Helper Functions
7
- //
8
-
9
- function isErrorObjectJaypieError(error) {
10
- if (error.isProjectError) {
11
- return {
12
- message: () => `expected "${error}" not to be a Jaypie error`,
13
- pass: true,
14
- };
15
- }
16
- return {
17
- message: () => `expected "${error}" to be a Jaypie error`,
18
- pass: false,
19
- };
20
- }
21
-
22
- //
23
- //
24
- // Main
25
- //
26
-
27
- const toBeJaypieError = (received) => {
28
- // See if it is an instance of error:
29
- if (received instanceof Error) {
30
- return isErrorObjectJaypieError(received);
31
- }
32
-
33
- const result = jsonSchemaMatchers.toMatchSchema(received, jsonApiErrorSchema);
34
- if (result.pass) {
35
- return {
36
- message: () => `expected ${received} not to be a Jaypie error`,
37
- pass: true,
38
- };
39
- } else {
40
- return {
41
- message: () => `expected ${received} to be a Jaypie error`,
42
- pass: false,
43
- };
44
- }
45
- };
46
-
47
- //
48
- //
49
- // Export
50
- //
51
-
52
- export default toBeJaypieError;
@@ -1,76 +0,0 @@
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
- });
@@ -1,111 +0,0 @@
1
- import {
2
- BadGatewayError,
3
- BadRequestError,
4
- ConfigurationError,
5
- ForbiddenError,
6
- GatewayTimeoutError,
7
- InternalError,
8
- isJaypieError,
9
- NotFoundError,
10
- UnauthorizedError,
11
- UnavailableError,
12
- } from "@jaypie/core";
13
-
14
- //
15
- //
16
- // Main
17
- //
18
-
19
- const toThrowJaypieError = async (received, expected) => {
20
- const isAsync =
21
- received.constructor.name === "AsyncFunction" ||
22
- received.constructor.name === "Promise";
23
-
24
- // If expected is a function, call it
25
- if (typeof expected === "function") {
26
- expected = expected();
27
- }
28
-
29
- try {
30
- const result = received();
31
-
32
- if (isAsync) {
33
- await result;
34
- }
35
-
36
- // If no error is thrown, fail the test
37
- return {
38
- pass: false,
39
- message: () =>
40
- "Expected function to throw a JaypieError, but it did not throw.",
41
- };
42
- } catch (error) {
43
- if (isJaypieError(error)) {
44
- // If expected is also a JaypieError, check if the error matches
45
- if (isJaypieError(expected)) {
46
- // If the error does not match, fail the test
47
- if (error._type !== expected._type) {
48
- return {
49
- pass: false,
50
- message: () =>
51
- `Expected function to throw "${expected._type}", but it threw "${error._type}"`,
52
- };
53
- }
54
- }
55
- return {
56
- pass: true,
57
- message: () =>
58
- `Expected function not to throw a JaypieError, but it threw ${error}`,
59
- };
60
- }
61
-
62
- return {
63
- pass: false,
64
- message: () =>
65
- `Expected function to throw a JaypieError, but it threw ${error}`,
66
- };
67
- }
68
- };
69
-
70
- //
71
- //
72
- // Convenience Methods
73
- //
74
-
75
- const toThrowBadGatewayError = (received) =>
76
- toThrowJaypieError(received, BadGatewayError);
77
- const toThrowBadRequestError = (received) =>
78
- toThrowJaypieError(received, BadRequestError);
79
- const toThrowConfigurationError = (received) =>
80
- toThrowJaypieError(received, ConfigurationError);
81
- const toThrowForbiddenError = (received) =>
82
- toThrowJaypieError(received, ForbiddenError);
83
- const toThrowGatewayTimeoutError = (received) =>
84
- toThrowJaypieError(received, GatewayTimeoutError);
85
- const toThrowInternalError = (received) =>
86
- toThrowJaypieError(received, InternalError);
87
- const toThrowNotFoundError = (received) =>
88
- toThrowJaypieError(received, NotFoundError);
89
- const toThrowUnauthorizedError = (received) =>
90
- toThrowJaypieError(received, UnauthorizedError);
91
- const toThrowUnavailableError = (received) =>
92
- toThrowJaypieError(received, UnavailableError);
93
-
94
- //
95
- //
96
- // Export
97
- //
98
-
99
- export default toThrowJaypieError;
100
-
101
- export {
102
- toThrowBadGatewayError,
103
- toThrowBadRequestError,
104
- toThrowConfigurationError,
105
- toThrowForbiddenError,
106
- toThrowGatewayTimeoutError,
107
- toThrowInternalError,
108
- toThrowNotFoundError,
109
- toThrowUnauthorizedError,
110
- toThrowUnavailableError,
111
- };
@@ -1,58 +0,0 @@
1
- import { matchers as jsonSchemaMatchers } from "jest-json-schema";
2
-
3
- import toBeCalledAboveTrace from "./matchers/toBeCalledAboveTrace.matcher.js";
4
- import toBeCalledWithInitialParams from "./matchers/toBeCalledWithInitialParams.matcher.js";
5
- import toBeClass from "./matchers/toBeClass.matcher.js";
6
- import toBeJaypieError from "./matchers/toBeJaypieError.matcher.js";
7
- import toThrowJaypieError, {
8
- toThrowBadGatewayError,
9
- toThrowBadRequestError,
10
- toThrowConfigurationError,
11
- toThrowForbiddenError,
12
- toThrowGatewayTimeoutError,
13
- toThrowInternalError,
14
- toThrowNotFoundError,
15
- toThrowUnauthorizedError,
16
- toThrowUnavailableError,
17
- } from "./matchers/toThrowJaypieError.matcher.js";
18
-
19
- import {
20
- toMatchBase64,
21
- toMatchJwt,
22
- toMatchMongoId,
23
- toMatchSignedCookie,
24
- toMatchUuid4,
25
- toMatchUuid5,
26
- toMatchUuid,
27
- } from "./matchers/toMatch.matcher.js";
28
-
29
- //
30
- //
31
- // Export
32
- //
33
-
34
- export default {
35
- toBeCalledAboveTrace,
36
- toBeCalledWithInitialParams,
37
- toBeClass,
38
- toBeJaypieError,
39
- toBeValidSchema: jsonSchemaMatchers.toBeValidSchema,
40
- toMatchBase64,
41
- toMatchJwt,
42
- toMatchMongoId,
43
- toMatchSchema: jsonSchemaMatchers.toMatchSchema,
44
- toMatchSignedCookie,
45
- toMatchUuid4,
46
- toMatchUuid5,
47
- toMatchUuid,
48
- toThrowBadGatewayError,
49
- toThrowBadRequestError,
50
- toThrowConfigurationError,
51
- toThrowForbiddenError,
52
- toThrowGatewayTimeoutError,
53
- toThrowInternalError,
54
- toThrowJaypieError,
55
- toThrowNotFoundError,
56
- toThrowUnauthorizedError,
57
- toThrowUnavailableError,
58
- };
@@ -1,99 +0,0 @@
1
- import { vi } from "vitest";
2
-
3
- export function mockLogFactory() {
4
- // Create skeleton of mock objects, as much as possible
5
- const mock = {
6
- debug: vi.fn(),
7
- error: vi.fn(),
8
- fatal: vi.fn(),
9
- info: vi.fn(),
10
- init: vi.fn(),
11
- lib: vi.fn(),
12
- tag: vi.fn(),
13
- trace: vi.fn(),
14
- untag: vi.fn(),
15
- var: vi.fn(),
16
- warn: vi.fn(),
17
- with: vi.fn(),
18
- };
19
- // Fill out nested mocks
20
- mock.debug.var = mock.var;
21
- mock.error.var = mock.var;
22
- mock.fatal.var = mock.var;
23
- mock.info.var = mock.var;
24
- mock.trace.var = mock.var;
25
- mock.warn.var = mock.var;
26
- // Have modules return correct objects
27
- mock.init.mockReturnValue(null);
28
- mock.lib.mockReturnValue(mock);
29
- mock.with.mockReturnValue(mock);
30
-
31
- // Create something in the shape of the module
32
- const module = {
33
- debug: mock.debug,
34
- error: mock.error,
35
- fatal: mock.fatal,
36
- info: mock.info,
37
- init: mock.init,
38
- lib: mock.lib,
39
- tag: mock.tag,
40
- trace: mock.trace,
41
- untag: mock.untag,
42
- var: mock.var,
43
- warn: mock.warn,
44
- with: mock.with,
45
- };
46
-
47
- // Pin mocks to the module
48
- module.mock = mock;
49
-
50
- // return the module
51
- return module;
52
- }
53
-
54
- // export default mockLogFactory();
55
-
56
- const logMethodNames = [
57
- "debug",
58
- "error",
59
- "fatal",
60
- "info",
61
- "init",
62
- "lib",
63
- "tag",
64
- "trace",
65
- "untag",
66
- "var",
67
- "warn",
68
- "with",
69
- ];
70
- const originalLogMethods = new WeakMap();
71
-
72
- export function spyLog(log) {
73
- if (!originalLogMethods.has(log)) {
74
- const mockLog = mockLogFactory();
75
- const originalMethods = {};
76
- logMethodNames.forEach((method) => {
77
- originalMethods[method] = log[method];
78
- log[method] = mockLog[method];
79
- });
80
- // Add custom properties
81
- originalMethods.mock = log.mock;
82
- log.mock = mockLog.mock;
83
-
84
- originalLogMethods.set(log, originalMethods);
85
- }
86
- }
87
-
88
- export function restoreLog(log) {
89
- const originalMethods = originalLogMethods.get(log);
90
- if (originalMethods) {
91
- logMethodNames.forEach((method) => {
92
- log[method] = originalMethods[method];
93
- });
94
- // Restore custom properties
95
- log.mock = originalMethods.mock;
96
-
97
- originalLogMethods.delete(log);
98
- }
99
- }
@@ -1,42 +0,0 @@
1
- import { v4 as uuid } from "uuid";
2
-
3
- //
4
- //
5
- // Main
6
- //
7
-
8
- const sqsTestRecords = (...args) => {
9
- const records = [];
10
-
11
- // See if only a single array was passed in and spread it
12
- if (args.length === 1 && Array.isArray(args[0])) {
13
- [args] = args;
14
- }
15
-
16
- for (let i = 0; i < args.length; i += 1) {
17
- const item = args[i];
18
- let body;
19
- if (item && typeof item === "object") {
20
- body = JSON.stringify(item);
21
- } else {
22
- // Cast item to string
23
- body = String(item);
24
- }
25
- const record = {
26
- messageId: uuid(),
27
- body,
28
- };
29
- records.push(record);
30
- }
31
-
32
- return {
33
- Records: records,
34
- };
35
- };
36
-
37
- //
38
- //
39
- // Export
40
- //
41
-
42
- export default sqsTestRecords;