@@ -71,7 +71,7 @@ describe("Utils", () => {
71
71
expect ( stringify ( [ { one : aFoo , two : aFoo } ] ) ) . toBe ( "[{\"one\":{\"a\":\"foo\"}}]" ) ;
72
72
} ) ;
73
73
74
- test . skip ( "deep circular reference" , ( ) => {
74
+ test ( "deep circular object reference" , ( ) => {
75
75
const a : { b ?: unknown } = { } ;
76
76
const b : { c ?: unknown } = { } ;
77
77
const c : { a ?: unknown , d : string } = { d : "test" } ;
@@ -86,6 +86,69 @@ describe("Utils", () => {
86
86
expect ( actual ) . toBe ( expected ) ;
87
87
} ) ;
88
88
89
+ test ( "deep circular array reference" , ( ) => {
90
+ type Circular = { circularRef ?: Circular , list ?: Circular [ ] } ;
91
+ const circular : Circular = { } ;
92
+ circular . circularRef = circular ;
93
+ circular . list = [ circular , circular ] ;
94
+
95
+ const expected = "{\"circularRef\":\"[Circular]\",\"list\":[\"[Circular]\",\"[Circular]\"]}" ;
96
+
97
+ const actual = stringify ( circular ) ;
98
+ expect ( actual ) . toBe ( expected ) ;
99
+ } ) ;
100
+
101
+ test ( "should serialize all data types" , ( ) => {
102
+ const value = {
103
+ "undefined" : undefined ,
104
+ "null" : null ,
105
+ "string" : "string" ,
106
+ "number" : 1 ,
107
+ "boolean" : true ,
108
+ "array" : [ 1 , 2 , 3 ] ,
109
+ "object" : { "a" : 1 , "b" : 2 , "c" : 3 } ,
110
+ "date" : new Date ( ) ,
111
+ "regex" : / r e g e x / ,
112
+ "function" : ( ) => { return undefined ; } ,
113
+ "error" : new Error ( "error" ) ,
114
+ "symbol" : Symbol ( "symbol" ) ,
115
+ "bigint" : BigInt ( 1 ) ,
116
+ "map" : new Map ( [ [ "a" , 1 ] , [ "b" , 2 ] , [ "c" , 3 ] ] ) ,
117
+ "weakMap" : new WeakMap ( [ [ { } , 1 ] , [ { } , 2 ] , [ { } , 3 ] ] ) ,
118
+ "set" : new Set ( [ 1 , 2 , 3 ] ) ,
119
+ "arrayBuffer" : new ArrayBuffer ( 1 ) ,
120
+ "dataView" : new DataView ( new ArrayBuffer ( 1 ) ) ,
121
+ "int8Array" : new Int8Array ( 1 ) ,
122
+ "uint8Array" : new Uint8Array ( 1 ) ,
123
+ "uint8ClampedArray" : new Uint8ClampedArray ( 1 ) ,
124
+ "int16Array" : new Int16Array ( 1 ) ,
125
+ "uint16Array" : new Uint16Array ( 1 ) ,
126
+ "int32Array" : new Int32Array ( 1 ) ,
127
+ "uint32Array" : new Uint32Array ( 1 ) ,
128
+ "float32Array" : new Float32Array ( 1 ) ,
129
+ "float64Array" : new Float64Array ( 1 ) ,
130
+ "bigint64Array" : new BigInt64Array ( 1 ) ,
131
+ "bigUint64Array" : new BigUint64Array ( 1 ) ,
132
+ "promise" : Promise . resolve ( 1 ) ,
133
+ "generator" : ( function * ( ) { yield 1 ; } ) ( ) ,
134
+ } ;
135
+
136
+ expect ( stringify ( value ) ) . toBe ( JSON . stringify ( value ) ) ;
137
+ } ) ;
138
+
139
+ test ( "should handle toJSON" , ( ) => {
140
+ const value = {
141
+ number : 1 ,
142
+ toJSON ( ) {
143
+ return {
144
+ test : "test"
145
+ } ;
146
+ }
147
+ } ;
148
+
149
+ expect ( stringify ( value ) ) . toBe ( JSON . stringify ( value ) ) ;
150
+ } ) ;
151
+
89
152
describe ( "should behave like JSON.stringify" , ( ) => {
90
153
[ new Date ( ) , 1 , true , null , undefined , ( ) => { return undefined ; } , user ] . forEach ( ( value ) => {
91
154
test ( "for " + typeof ( value ) , ( ) => {
@@ -94,21 +157,30 @@ describe("Utils", () => {
94
157
} ) ;
95
158
} ) ;
96
159
97
- /*
98
- test.skip("should respect maxDepth", () => {
99
- const deepObject = {
100
- a: {
101
- b: {
102
- c: {
103
- d: {}
160
+ test ( "should respect maxDepth" , ( ) => {
161
+ const value = {
162
+ ao : {
163
+ bo : {
164
+ cn : 1 ,
165
+ co : {
166
+ do : { }
167
+ }
168
+ } ,
169
+ ba : [
170
+ {
171
+ cn : 1 ,
172
+ co : {
173
+ do : { }
174
+ }
104
175
}
105
- }
176
+ ] ,
177
+ bn : 1
106
178
}
107
179
} ;
108
180
109
- expect(deepObject).toBe("TODO");
181
+ expect ( stringify ( value , undefined , 1 ) ) . toBe ( JSON . stringify ( { ao : { bn : 1 } } ) ) ;
182
+ expect ( stringify ( value , undefined , 2 ) ) . toBe ( JSON . stringify ( { ao : { bo : { cn : 1 } , bn : 1 } } ) ) ;
110
183
} ) ;
111
- */
112
184
113
185
test ( "should serialize inherited properties" , ( ) => {
114
186
// @ts -expect-error TS2683
0 commit comments