Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

json.test.ts

142 lines4.8 KB

1import { describe, expect, it } from 'vitest'
2import { parseWithBigInt, stringifyWithBigInt } from './json'
3​
4describe('json utils', () => {
5 describe('stringifyWithBigInt', () => {
6 it('should handle regular JSON data', () => {
7 const data = { name: 'test', count: 42, nested: { value: true } }
8 const result = stringifyWithBigInt(data)
9 expect(result).toBe(JSON.stringify(data))
10 })
11​
12 it('should convert BigInt to object with marker', () => {
13 const data = { id: BigInt(9007199254740991) }
14 const result = stringifyWithBigInt(data)
15 const parsed = JSON.parse(result)
16 expect(parsed.id).toEqual({
17 __type: 'bigint',
18 value: '9007199254740991',
19 })
20 })
21​
22 it('should handle nested BigInt values', () => {
23 const data = {
24 user: { id: BigInt(123), balance: BigInt(456789) },
25 timestamp: BigInt(1234567890),
26 }
27 const result = stringifyWithBigInt(data)
28 const parsed = JSON.parse(result)
29 expect(parsed.user.id).toEqual({ __type: 'bigint', value: '123' })
30 expect(parsed.user.balance).toEqual({
31 __type: 'bigint',
32 value: '456789',
33 })
34 expect(parsed.timestamp).toEqual({
35 __type: 'bigint',
36 value: '1234567890',
37 })
38 })
39​
40 it('should handle arrays with BigInt', () => {
41 const data = [BigInt(1), BigInt(2), BigInt(3)]
42 const result = stringifyWithBigInt(data)
43 const parsed = JSON.parse(result)
44 expect(parsed).toEqual([
45 { __type: 'bigint', value: '1' },
46 { __type: 'bigint', value: '2' },
47 { __type: 'bigint', value: '3' },
48 ])
49 })
50​
51 it('should handle mixed types with BigInt', () => {
52 const data = {
53 string: 'text',
54 number: 42,
55 bigint: BigInt(999),
56 boolean: true,
57 null: null,
58 array: [1, BigInt(2), 'three'],
59 }
60 const result = stringifyWithBigInt(data)
61 const parsed = JSON.parse(result)
62 expect(parsed.bigint).toEqual({ __type: 'bigint', value: '999' })
63 expect(parsed.array[1]).toEqual({ __type: 'bigint', value: '2' })
64 })
65 })
66​
67 describe('parseWithBigInt', () => {
68 it('should handle regular JSON data', () => {
69 const json = JSON.stringify({ name: 'test', count: 42 })
70 const result = parseWithBigInt(json)
71 expect(result).toEqual({ name: 'test', count: 42 })
72 })
73​
74 it('should restore BigInt from marker object', () => {
75 const json = JSON.stringify({ id: { __type: 'bigint', value: '123' } })
76 const result = parseWithBigInt(json)
77 expect(result.id).toBe(BigInt(123))
78 expect(typeof result.id).toBe('bigint')
79 })
80​
81 it('should handle nested BigInt restoration', () => {
82 const json = JSON.stringify({
83 user: {
84 id: { __type: 'bigint', value: '9007199254740991' },
85 balance: { __type: 'bigint', value: '456789' },
86 },
87 timestamp: { __type: 'bigint', value: '1234567890' },
88 })
89 const result = parseWithBigInt(json)
90 expect(result.user.id).toBe(BigInt(9007199254740991))
91 expect(result.user.balance).toBe(BigInt(456789))
92 expect(result.timestamp).toBe(BigInt(1234567890))
93 })
94​
95 it('should handle arrays with BigInt restoration', () => {
96 const json = JSON.stringify([
97 { __type: 'bigint', value: '1' },
98 { __type: 'bigint', value: '2' },
99 { __type: 'bigint', value: '3' },
100 ])
101 const result = parseWithBigInt(json)
102 expect(result).toEqual([BigInt(1), BigInt(2), BigInt(3)])
103 })
104​
105 it('should not convert objects that look like BigInt markers but are not', () => {
106 const json = JSON.stringify({
107 fake: { __type: 'bigint', value: 123 }, // value is not a string
108 real: { __type: 'bigint', value: '456' },
109 })
110 const result = parseWithBigInt(json)
111 expect(result.fake).toEqual({ __type: 'bigint', value: 123 })
112 expect(result.real).toBe(BigInt(456))
113 })
114 })
115​
116 describe('round-trip', () => {
117 it('should correctly round-trip BigInt values', () => {
118 const original = {
119 id: BigInt(9007199254740991),
120 nested: {
121 value: BigInt(123456789),
122 },
123 array: [BigInt(1), BigInt(2)],
124 }
125 const stringified = stringifyWithBigInt(original)
126 const parsed = parseWithBigInt(stringified)
127 expect(parsed.id).toBe(original.id)
128 expect(parsed.nested.value).toBe(original.nested.value)
129 expect(parsed.array[0]).toBe(original.array[0])
130 expect(parsed.array[1]).toBe(original.array[1])
131 })
132​
133 it('should handle very large BigInt values', () => {
134 const original = {
135 huge: BigInt('123456789012345678901234567890'),
136 }
137 const stringified = stringifyWithBigInt(original)
138 const parsed = parseWithBigInt(stringified)
139 expect(parsed.huge).toBe(original.huge)
140 })
141 })
142})