helpers.test.js 909 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import test from 'tape';
  2. import { jsonDump } from '#/injected/web/util-web';
  3. test('jsonDump', (t) => {
  4. const sameChildObj = { foo: 1 };
  5. // eslint-disable-next-line no-restricted-syntax
  6. for (const obj of [
  7. 1,
  8. null,
  9. false,
  10. undefined,
  11. Infinity,
  12. NaN,
  13. 'abc',
  14. {},
  15. [],
  16. [1, 2, 3, undefined, , 4], // eslint-disable-line no-sparse-arrays
  17. {
  18. a: 1, b: '2', c: true, d: 'aaa',
  19. },
  20. {
  21. a: [1, 2, 3],
  22. b: { a: '\\"\x01foo\r\t"\u2028\u2029' },
  23. skipped: undefined,
  24. unsupported: new Set(),
  25. }, {
  26. sameChild1: sameChildObj,
  27. sameChild2: sameChildObj,
  28. sameChild3: [sameChildObj],
  29. },
  30. ]) {
  31. t.equal(jsonDump(obj), JSON.stringify(obj));
  32. }
  33. t.throws(() => {
  34. const cyclic = {};
  35. cyclic.foo = [1, 2, 3, { cyclic }];
  36. jsonDump(cyclic);
  37. }, /Converting circular structure to JSON/, 'circular');
  38. t.end();
  39. });