forked from duailibe/jest-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (129 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
const diff = require("jest-diff");
const { equals } = require("expect/build/jasmine_utils");
const { isOneline } = require("expect/build/utils");
const {
RECEIVED_COLOR,
matcherHint,
printExpected,
printReceived,
printWithType
} = require("jest-matcher-utils");
/**
* Jest matcher that receives a JSON string and matches to a value.
*
* expect(fooJson).toMatchJSON(expected)
*/
function toMatchJSON(actual, expected) {
const hint = matcherHint(".toMatchJSON", "string", "expected", {
isNot: this.isNot
});
const prefix = hint + "\n\n" + RECEIVED_COLOR("string") + " ";
if (typeof actual !== "string") {
throwError(
prefix +
"value must be a string.\n" +
printWithType("Received", actual, printReceived)
);
}
if (!actual) {
throwError(
prefix +
"value must be a valid JSON.\nReceived:\n " +
printReceived(actual)
);
}
try {
actual = JSON.parse(actual);
} catch (err) {
throwError(
prefix +
"value must be a valid JSON.\n" +
printInvalid(actual, err.message)
);
}
const pass = equals(actual, expected);
const message = pass
? () =>
matcherHint(".not.toMatchJSON") +
"\n\nExpected value not to match:\n " +
printExpected(expected) +
"\nReceived:\n " +
printReceived(actual)
: () => {
const oneline = isOneline(expected, actual);
const diffString =
!oneline && diff(expected, actual, { expand: this.expand });
return (
matcherHint(".toMatchJSON") +
"\n\nExpected value to match:\n " +
printExpected(expected) +
"\nReceived:\n " +
printReceived(actual) +
(diffString && !oneline ? "\n\nDifference: \n\n" + diffString : "")
);
};
return { pass, message };
}
/**
* Asymmetric matcher to check the format of a JSON string.
*
* expect({ foo: fooJson }).toEqual({
* foo: expect.jsonMatching(expected),
* })
*/
function jsonMatching(actual, expected) {
const _this = expect.jsonMatching();
if (typeof actual !== "string") {
throw Error(
`You must provide a string to ${_this.toString()}, not '${typeof actual}'.`
);
}
try {
actual = JSON.parse(actual);
} catch (err) {
throw Error("Actual is not valid JSON");
}
return { pass: equals(actual, expected) };
}
expect.extend({ jsonMatching, toMatchJSON });
/**
* Formats the JSON.parse error message
*/
function printInvalid(received, error) {
const match = error.match(
/Unexpected (\w+)(?: .)? in JSON at position (\d+)/
);
const message = "Received:\n " + printReceived(received) + "\n";
if (match) {
const pos = parseInt(match[2], 10);
return (
message +
" ".repeat(pos + 3) +
"^\nUnexpected " +
match[1] +
": " +
RECEIVED_COLOR(received[pos])
);
}
return (
message +
" ".repeat(received.length + 3) +
"^\n" +
"Unexpected end of string\n"
);
}
/**
* Throws the errors removing the matcher from stack trace
*/
function throwError(message) {
try {
throw Error(message);
} catch (err) {
message = message || "Error";
const stack = err.stack.slice(message.length).split("\n");
stack.splice(0, 4, message);
err.stack = stack.join("\n");
throw err;
}
}