Skip to content

Commit ef9be7f

Browse files
committed
test: add more node.js tests
1 parent 3822686 commit ef9be7f

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

test/node.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import vm from "node:vm";
22

3+
function extendedFunction() {}
4+
extendedFunction.prototype = { a: "b" };
5+
36
function createWithNoPrototype(properties) {
47
const noProto = Object.create(null);
58
properties.forEach((property) => {
@@ -112,3 +115,20 @@ export const qsTestCases = [
112115
[null, "", {}],
113116
[undefined, "", {}],
114117
];
118+
export const qsWeirdObjects = [
119+
[{ regexp: /./g }, "regexp=", { regexp: "" }],
120+
[{ regexp: new RegExp(".", "g") }, "regexp=", { regexp: "" }],
121+
[{ fn: () => {} }, "fn=", { fn: "" }],
122+
[{ fn: new Function("") }, "fn=", { fn: "" }],
123+
[{ math: Math }, "math=", { math: "" }],
124+
[{ e: extendedFunction }, "e=", { e: "" }],
125+
[{ d: new Date() }, "d=", { d: "" }],
126+
[{ d: Date }, "d=", { d: "" }],
127+
[{ f: new Boolean(false), t: new Boolean(true) }, "f=&t=", { f: "", t: "" }],
128+
[{ f: false, t: true }, "f=false&t=true", { f: "false", t: "true" }],
129+
[{ n: null }, "n=", { n: "" }],
130+
[{ nan: NaN }, "nan=", { nan: "" }],
131+
[{ inf: Infinity }, "inf=", { inf: "" }],
132+
[{ a: [], b: [] }, "", {}],
133+
[{ a: 1, b: [] }, "a=1", { a: "1" }],
134+
];

test/parse.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import qs from "../lib";
22
import { test, assert } from "vitest";
3-
import { qsNoMungeTestCases, qsTestCases } from "./node";
3+
import { qsNoMungeTestCases, qsTestCases, qsWeirdObjects } from "./node";
44
import querystring from "querystring";
55

66
test("should succeed on node.js tests", () => {
7+
qsWeirdObjects.forEach(
8+
(t) =>
9+
assert.deepEqual(qs.parse(t[1] as string), t[2] as Record<string, any>),
10+
);
711
qsNoMungeTestCases.forEach((t) => assert.deepEqual(qs.parse(t[0]), t[1]));
812
qsTestCases.forEach((t) => assert.deepEqual(qs.parse(t[0]), t[2]));
913
});
@@ -39,3 +43,13 @@ test("should accept pairs with missing values", () => {
3943
test("should decode key", () => {
4044
assert.deepEqual(qs.parse("full%20name=Yagiz"), { "full name": "Yagiz" });
4145
});
46+
47+
test("should handle really large object", () => {
48+
const query = {};
49+
50+
for (let i = 0; i < 2000; i++) query[i] = i;
51+
52+
const url = qs.stringify(query);
53+
54+
assert.strictEqual(Object.keys(qs.parse(url)).length, 2000);
55+
});

test/stringify.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { qsNoMungeTestCases, qsTestCases } from "./node";
1+
import { qsNoMungeTestCases, qsTestCases, qsWeirdObjects } from "./node";
22
import qs from "../lib";
33
import { test, assert } from "vitest";
44
import querystring from "querystring";
55

66
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+
);
714
qsNoMungeTestCases.forEach((t) => assert.deepEqual(qs.stringify(t[1]), t[0]));
815
qsTestCases.forEach((t) => assert.deepEqual(qs.stringify(t[2]), t[1]));
916
});
@@ -29,6 +36,8 @@ test("should handle BigInt", () => {
2936
qs.stringify({ age: BigInt(55), name: "John" }),
3037
"age=55&name=John",
3138
);
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");
3241
});
3342

3443
test("should handle boolean values", () => {
@@ -52,3 +61,36 @@ test("should omit non-object inputs", () => {
5261
test("should handle multi-byte characters", () => {
5362
assert.deepEqual(qs.stringify({ multiByte: "𝌆" }), "multiByte=%F0%9D%8C%86");
5463
});
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

Comments
 (0)