JSON.parse.js 827 B

12345678910111213141516171819202122232425262728293031323334353637
  1. test("basic functionality", () => {
  2. expect(JSON.parse).toHaveLength(2);
  3. const properties = [
  4. ["5", 5],
  5. ["null", null],
  6. ["true", true],
  7. ["false", false],
  8. ['"test"', "test"],
  9. ['[1,2,"foo"]', [1, 2, "foo"]],
  10. ['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }],
  11. ];
  12. properties.forEach(testCase => {
  13. expect(JSON.parse(testCase[0])).toEqual(testCase[1]);
  14. });
  15. });
  16. test("syntax errors", () => {
  17. [
  18. undefined,
  19. NaN,
  20. -NaN,
  21. Infinity,
  22. -Infinity,
  23. '{ "foo" }',
  24. '{ foo: "bar" }',
  25. "[1,2,3,]",
  26. "[1,2,3, ]",
  27. '{ "foo": "bar",}',
  28. '{ "foo": "bar", }',
  29. ].forEach(test => {
  30. expect(() => {
  31. JSON.parse(test);
  32. }).toThrow(SyntaxError);
  33. });
  34. });