UNPKG

json-restructure

Version:

A TypeScript library for repairing malformed JSON strings

38 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jsonParser_1 = require("../../src/utils/jsonParser"); const types_1 = require("../../src/types"); describe('parseJson', () => { it('should parse a valid JSON string', () => { const validJson = '{"name":"John","age":30}'; const result = (0, jsonParser_1.parseJson)(validJson); expect(result).toEqual({ name: 'John', age: 30 }); }); it('should throw a JsonParseError for invalid JSON', () => { const invalidJson = '{name:"John",age:30}'; expect(() => (0, jsonParser_1.parseJson)(invalidJson)).toThrow(types_1.JsonParseError); try { (0, jsonParser_1.parseJson)(invalidJson); } catch (error) { if (error instanceof types_1.JsonParseError) { expect(error.lineNumber).toBe(1); expect(error.columnNumber).toBe(2); } } }); it('should extract error details correctly', () => { const invalidJson = '{\n "name": "John",\n age: 30\n}'; try { (0, jsonParser_1.parseJson)(invalidJson); } catch (error) { if (error instanceof types_1.JsonParseError) { expect(error.lineNumber).toBe(3); expect(error.columnNumber).toBe(2); expect(error.message).toBe("JsonParseError: Unexpected token 'a' in JSON at line 3, column 2 (line 3, column 2)"); } } }); }); //# sourceMappingURL=jsonParser.test.js.map