Skip to content

Commit 1a2fc54

Browse files
committed
beef up tests
1 parent 8722e75 commit 1a2fc54

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

test/test.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -390,55 +390,63 @@ for (const [name, tests] of Object.entries(fixtures)) {
390390
test.run();
391391
}
392392

393-
const syntaxErrorFixtures = [
393+
const invalid = [
394394
{
395395
name: 'empty string',
396-
json: ''
396+
json: '',
397+
message: 'Unexpected end of JSON input'
397398
},
398399
{
399400
name: 'invalid JSON',
400-
json: ']['
401+
json: '][',
402+
message: 'Unexpected token ] in JSON at position 0'
401403
},
402404
{
403405
name: 'hole',
404-
json: '-2'
406+
json: '-2',
407+
message: 'Unexpected number -2'
405408
},
406409
{
407410
name: 'string',
408411
json: '"hello"',
412+
message: 'Expected array, got string'
409413
},
410414
{
411415
name: 'number',
412-
json: '42'
416+
json: '42',
417+
message: 'Unexpected number 42'
413418
},
414419
{
415420
name: 'boolean',
416-
json: 'true'
421+
json: 'true',
422+
message: 'Expected array, got boolean'
417423
},
418424
{
419425
name: 'null',
420-
json: 'null'
426+
json: 'null',
427+
message: 'Expected array, got null'
421428
},
422429
{
423430
name: 'object',
424-
json: '{}'
431+
json: '{}',
432+
message: 'Expected array, got object'
425433
},
426434
{
427435
name: 'empty array',
428-
json: '[]'
436+
json: '[]',
437+
message: 'Unexpected empty array'
429438
}
430439
];
431440

432-
const syntaxErrorTest = uvu.suite("parse: syntax errors");
433-
434-
for (const fixture of syntaxErrorFixtures) {
435-
syntaxErrorTest(fixture.name, () => {
436-
assert.throws(() => parse(fixture.json), (error) => error instanceof SyntaxError);
441+
for (const { name, json, message } of invalid) {
442+
uvu.test(`parse error: ${name}`, () => {
443+
assert.throws(
444+
() => parse(json),
445+
(error) => error instanceof SyntaxError && error.message === message
446+
);
437447
});
438448
}
439449

440-
syntaxErrorTest.run();
441-
442450
for (const fn of [uneval, stringify]) {
443451
uvu.test(`${fn.name} throws for non-POJOs`, () => {
444452
class Foo {}

0 commit comments

Comments
 (0)