1
- import { qsNoMungeTestCases , qsTestCases } from "./node" ;
1
+ import { qsNoMungeTestCases , qsTestCases , qsWeirdObjects } from "./node" ;
2
2
import qs from "../lib" ;
3
3
import { test , assert } from "vitest" ;
4
4
import querystring from "querystring" ;
5
5
6
6
test ( "should succeed on node.js tests" , ( ) => {
7
+ qsWeirdObjects . forEach (
8
+ ( t ) =>
9
+ assert . deepEqual (
10
+ qs . stringify ( t [ 2 ] as Record < string , any > ) ,
11
+ t [ 1 ] as string ,
12
+ ) ,
13
+ ) ;
7
14
qsNoMungeTestCases . forEach ( ( t ) => assert . deepEqual ( qs . stringify ( t [ 1 ] ) , t [ 0 ] ) ) ;
8
15
qsTestCases . forEach ( ( t ) => assert . deepEqual ( qs . stringify ( t [ 2 ] ) , t [ 1 ] ) ) ;
9
16
} ) ;
@@ -29,6 +36,8 @@ test("should handle BigInt", () => {
29
36
qs . stringify ( { age : BigInt ( 55 ) , name : "John" } ) ,
30
37
"age=55&name=John" ,
31
38
) ;
39
+ assert . strictEqual ( qs . stringify ( { foo : 2n ** 1023n } ) , "foo=" + 2n ** 1023n ) ;
40
+ assert . strictEqual ( qs . stringify ( [ 0n , 1n , 2n ] ) , "0=0&1=1&2=2" ) ;
32
41
} ) ;
33
42
34
43
test ( "should handle boolean values" , ( ) => {
@@ -52,3 +61,36 @@ test("should omit non-object inputs", () => {
52
61
test ( "should handle multi-byte characters" , ( ) => {
53
62
assert . deepEqual ( qs . stringify ( { multiByte : "𝌆" } ) , "multiByte=%F0%9D%8C%86" ) ;
54
63
} ) ;
64
+
65
+ test ( "invalid surrogate pair should throw" , ( ) => {
66
+ assert . throws ( ( ) => qs . stringify ( { foo : "\udc00" } ) , "URI malformed" ) ;
67
+ } ) ;
68
+
69
+ test ( "should omit nested values" , ( ) => {
70
+ const f = qs . stringify ( {
71
+ a : "b" ,
72
+ q : qs . stringify ( {
73
+ x : "y" ,
74
+ y : "z" ,
75
+ } ) ,
76
+ } ) ;
77
+ assert . strictEqual ( f , "a=b&q=x%3Dy%26y%3Dz" ) ;
78
+ } ) ;
79
+
80
+ test ( "should coerce numbers to string" , ( ) => {
81
+ assert . strictEqual ( qs . stringify ( { foo : 0 } ) , "foo=0" ) ;
82
+ assert . strictEqual ( qs . stringify ( { foo : - 0 } ) , "foo=0" ) ;
83
+ assert . strictEqual ( qs . stringify ( { foo : 3 } ) , "foo=3" ) ;
84
+ assert . strictEqual ( qs . stringify ( { foo : - 72.42 } ) , "foo=-72.42" ) ;
85
+ assert . strictEqual ( qs . stringify ( { foo : NaN } ) , "foo=" ) ;
86
+ assert . strictEqual ( qs . stringify ( { foo : 1e21 } ) , "foo=1e%2B21" ) ;
87
+ assert . strictEqual ( qs . stringify ( { foo : Infinity } ) , "foo=" ) ;
88
+ } ) ;
89
+
90
+ test ( "should return empty string on certain inputs" , ( ) => {
91
+ assert . strictEqual ( qs . stringify ( ) , "" ) ;
92
+ assert . strictEqual ( qs . stringify ( 0 ) , "" ) ;
93
+ assert . strictEqual ( qs . stringify ( [ ] ) , "" ) ;
94
+ assert . strictEqual ( qs . stringify ( null ) , "" ) ;
95
+ assert . strictEqual ( qs . stringify ( true ) , "" ) ;
96
+ } ) ;
0 commit comments