-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
143 lines (127 loc) · 3.46 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
141
142
143
"use strict";
const errorMarker = "__INTERNALERRORMARKER__";
/**
* Jest matcher that receives a JSON string and matches to a value.
*
* expect(fooJson).toMatchJSON(expected)
*/
function toMatchJSON(received, expected) {
const {
matcherErrorMessage,
RECEIVED_COLOR,
printDiffOrStringify,
printExpected,
printReceived,
printWithType,
stringify,
} = this.utils;
const hint = this.utils.matcherHint("toMatchJSON", undefined, undefined, {
isNot: this.isNot,
promise: this.promise,
});
if (typeof received !== "string") {
throwError(
matcherErrorMessage(
hint,
`${RECEIVED_COLOR("received")} value must be a valid JSON string`,
printWithType("Received", received, printReceived)
)
);
}
try {
received = JSON.parse(received);
} catch (error) {
const match = error.message.match(
/Unexpected (\w+)(?: .)? in JSON at position (\d+)/
);
const index = match ? parseInt(match[2], 10) : received.length;
const isEmpty = received.trim().length === 0;
const message = isEmpty
? ""
: match
? `Unexpected ${match[1]}: ${received[index]}`
: "Unexpected end of string";
throwError(
matcherErrorMessage(
hint,
`${RECEIVED_COLOR(
"received"
)} value must be a valid JSON string. ${message}`,
isEmpty
? "Received: " + RECEIVED_COLOR(stringify(received))
: printJsonError(stringify(received), RECEIVED_COLOR, index + 1)
)
);
}
const pass = this.equals(received, expected);
const message = pass
? () =>
`${hint} \n\nExpected: not ${printExpected(expected)}` +
(stringify(expected) !== stringify(received)
? `\nReceived: ${printReceived(received)}`
: "")
: () =>
`${hint} \n\n` +
printDiffOrStringify(
expected,
received,
"Expected",
"Received",
this.expand !== false
);
return { pass, message };
}
/**
* Asymmetric matcher to check the format of a JSON string.
*
* expect({ foo: fooJson }).toEqual({
* foo: expect.jsonMatching(expected),
* })
*/
function jsonMatching(received, expected) {
let pass = false;
try {
received = JSON.parse(received);
pass = this.equals(received, expected);
} catch (err) {} // eslint-disable-line no-empty
return { pass };
}
expect.extend({ jsonMatching, toMatchJSON });
/**
* Formats the JSON.parse error message
*/
function printJsonError(value, print, index) {
let message = `Received: `;
const lines = value.split("\n");
for (let i = 0, count = 0; i < lines.length; i++) {
const line = lines[i];
message += print(line) + "\n";
if (index >= count && index <= count + line.length) {
message += " ".repeat(index - count + (i === 0 ? 10 : 0)) + "^\n";
}
count += line.length + 1;
}
return message;
}
/**
* Throws the errors removing the matcher from stack trace
*/
function throwError(message) {
try {
throw Error(errorMarker);
} catch (err) {
const stack = err.stack
.slice(err.stack.indexOf(errorMarker) + errorMarker.length)
.split("\n");
for (let i = stack.length - 1; i > 0; i--) {
// search for the "first" matcher call in the trace
if (stack[i].includes("toMatchJSON")) {
stack.splice(0, i + 1, message);
break;
}
}
const error = Error(message);
error.stack = stack.join("\n");
throw error;
}
}