jan/web/utils/json.test.ts
Louis 302b73ae73
test: add web helpers, services, utils tests (#3669)
* test: add web helpers tests

* fix: coverage report

* test: add more tests

* test: add more generated tests

* chore: add more tests

* test: add more tests
2024-09-20 14:24:51 +07:00

22 lines
770 B
TypeScript

// json.test.ts
import { safeJsonParse } from './json';
describe('safeJsonParse', () => {
it('should correctly parse a valid JSON string', () => {
const jsonString = '{"name": "John", "age": 30}';
const result = safeJsonParse<{ name: string; age: number }>(jsonString);
expect(result).toEqual({ name: 'John', age: 30 });
});
it('should return undefined for an invalid JSON string', () => {
const jsonString = '{"name": "John", "age": 30';
const result = safeJsonParse<{ name: string; age: number }>(jsonString);
expect(result).toBeUndefined();
});
it('should return undefined for an empty string', () => {
const jsonString = '';
const result = safeJsonParse<unknown>(jsonString);
expect(result).toBeUndefined();
});
});