-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
test.js
131 lines (116 loc) · 2.85 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
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,
});
});
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');
}
}
});