Skip to content

Commit 4d9f797

Browse files
jdaltonclaude
andcommitted
Improve json.ts test coverage
Add tests for Buffer parsing, BOM stripping, filepath error messages, and reviver function. This improves json.ts coverage from 89.13% to 91.3% (100% function coverage). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f184424 commit 4d9f797

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

test/registry/json.test.mts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,38 @@ describe('json module', () => {
106106
expect(jsonParse('{"empty": []}')).toEqual({ empty: [] })
107107
expect(jsonParse('{"empty": {}}')).toEqual({ empty: {} })
108108
})
109+
110+
it('should parse JSON from Buffer', () => {
111+
const buffer = Buffer.from('{"key": "value"}')
112+
expect(jsonParse(buffer)).toEqual({ key: 'value' })
113+
})
114+
115+
it('should strip BOM from Buffer', () => {
116+
const bomBuffer = Buffer.from('\uFEFF{"key": "value"}')
117+
expect(jsonParse(bomBuffer)).toEqual({ key: 'value' })
118+
})
119+
120+
it('should include filepath in error message when provided', () => {
121+
expect(() =>
122+
jsonParse('{invalid}', { filepath: '/path/to/file.json' }),
123+
).toThrow('/path/to/file.json')
124+
})
125+
126+
it('should use reviver function when provided', () => {
127+
const json = '{"date": "2023-01-01T00:00:00.000Z"}'
128+
const reviver = (key: string, value: any) => {
129+
if (key === 'date') {
130+
return new Date(value)
131+
}
132+
return value
133+
}
134+
const result = jsonParse(json, { reviver }) as any
135+
expect(result.date).toBeInstanceOf(Date)
136+
expect(result.date.toISOString()).toBe('2023-01-01T00:00:00.000Z')
137+
})
138+
139+
it('should handle throws option explicitly set to true', () => {
140+
expect(() => jsonParse('{invalid}', { throws: true })).toThrow()
141+
})
109142
})
110143
})

0 commit comments

Comments
 (0)