-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest.js
185 lines (162 loc) · 4.03 KB
/
test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import process from 'node:process';
import test from 'ava';
import {outdent} from 'outdent';
import stripAnsi from 'strip-ansi';
import parseJson, {JSONError} from './index.js';
const NODE_JS_VERSION = Number(process.versions.node.split('.')[0]);
const errorMessageRegex = (() => {
if (NODE_JS_VERSION < 20) {
return /Unexpected token "}"\(\\u{7d}\) in JSON at position 16/;
}
if (NODE_JS_VERSION < 21) {
return /Expected double-quoted property name in JSON at position 16/;
}
return /Expected double-quoted property name in JSON at position 16 \(line 3 column 1\)/;
})();
const errorMessageRegexWithFileName = new RegExp(errorMessageRegex.source + '.*in foo\\.json');
const INVALID_JSON_STRING = outdent`
{
"foo": true,
}
`;
const EXPECTED_CODE_FRAME = `
1 | {
2 | "foo": true,
> 3 | }
| ^
`.slice(1, -1);
test('main', t => {
t.deepEqual(parseJson('{"foo": true}'), {foo: true});
t.throws(() => {
parseJson(INVALID_JSON_STRING);
}, {
name: 'JSONError',
message: errorMessageRegex,
});
t.throws(() => {
try {
parseJson(INVALID_JSON_STRING);
} catch (error) {
error.fileName = 'foo.json';
throw error;
}
}, {
message: errorMessageRegexWithFileName,
});
t.throws(() => {
parseJson(INVALID_JSON_STRING, 'foo.json');
}, {
message: errorMessageRegexWithFileName,
});
t.throws(() => {
try {
parseJson(INVALID_JSON_STRING, 'bar.json');
} catch (error) {
error.fileName = 'foo.json';
throw error;
}
}, {
message: errorMessageRegexWithFileName,
});
{
let jsonError;
try {
parseJson(INVALID_JSON_STRING, 'foo.json');
} catch (error) {
jsonError = error;
}
jsonError.message = 'custom error message';
t.true(jsonError.message.startsWith('custom error message in foo.json'));
// Still have code frame in message.
t.true(stripAnsi(jsonError.message).includes('> 3 | }'));
}
{
let nativeJsonParseError;
try {
JSON.parse(INVALID_JSON_STRING);
} catch (error) {
nativeJsonParseError = error;
}
let jsonError;
try {
parseJson(INVALID_JSON_STRING);
} catch (error) {
jsonError = error;
}
t.is(nativeJsonParseError.name, 'SyntaxError');
t.deepEqual(nativeJsonParseError, jsonError.cause);
}
});
test('throws exported error error', t => {
t.throws(() => {
parseJson('asdf');
}, {
instanceOf: JSONError,
});
});
test('has error frame properties', t => {
try {
parseJson(INVALID_JSON_STRING, 'foo.json');
} catch (error) {
t.is(error.rawCodeFrame, EXPECTED_CODE_FRAME);
t.is(stripAnsi(error.codeFrame), EXPECTED_CODE_FRAME);
}
});
test('allow error location out of bounds', t => {
try {
parseJson('{');
} catch (error) {
t.true(error instanceof JSONError);
t.is(error.rawCodeFrame, NODE_JS_VERSION === 18 ? undefined : outdent`
> 1 | {
| ^
`);
}
});
test('empty string', t => {
try {
parseJson('');
} catch (error) {
t.true(error instanceof JSONError);
t.is(error.message, 'Unexpected end of JSON input while parsing empty string');
t.is(error.rawCodeFrame, undefined);
}
try {
parseJson(' ');
} catch (error) {
t.true(error instanceof JSONError);
t.is(error.message, 'Unexpected end of JSON input');
t.is(error.rawCodeFrame, undefined);
}
});
test('Unexpected tokens', t => {
try {
parseJson('a');
} catch (error) {
t.true(error instanceof JSONError);
const firstLine = error.message.split('\n')[0];
if (NODE_JS_VERSION === 18) {
t.is(firstLine, 'Unexpected token "a"(\\u{61}) in JSON at position 0');
} else {
t.is(firstLine, 'Unexpected token "a"(\\u{61}), "a" is not valid JSON');
}
}
});
test('JSONError legacy interface', t => {
{
const error = new JSONError('Error message');
t.is(error.message, 'Error message');
}
{
const error = new JSONError('Error message');
error.message = 'New error message';
t.is(error.message, 'New error message');
}
{
const error = new JSONError('Error message');
error.fileName = 'foo.json';
t.is(error.message, 'Error message in foo.json');
error.message = 'New error message';
t.is(error.message, 'New error message in foo.json');
}
});