Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 9d7067a

Browse files
committed
Optimize test code
1 parent 991e86b commit 9d7067a

File tree

2 files changed

+218
-369
lines changed

2 files changed

+218
-369
lines changed

__tests__/json-stringify.test.ts

Lines changed: 109 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,64 @@
11
import { jsonStringify } from '../src/index';
22

3-
describe('Serializing a single value returns the same result as JSON.stringfy()', () => {
4-
test('undefined', () => {
5-
const expectedResult = JSON.stringify(undefined);
6-
const result = jsonStringify(undefined);
7-
8-
expect(result).toEqual(expectedResult);
3+
describe('Serializing', () => {
4+
test('undefined returns undefined', () => {
5+
const output = jsonStringify(undefined);
6+
expect(output).toEqual(undefined);
97
});
108

11-
test('boolean', () => {
12-
const expectedResult = JSON.stringify(true);
13-
const result = jsonStringify(true);
14-
15-
expect(result).toEqual(expectedResult);
9+
test('null returns \'null\'', () => {
10+
const output = jsonStringify(null);
11+
expect(output).toEqual('null');
1612
});
1713

18-
test('number', () => {
19-
const expectedResult = JSON.stringify(42);
20-
const result = jsonStringify(42);
21-
22-
expect(result).toEqual(expectedResult);
14+
test('true (boolean) returns \'true\'', () => {
15+
const output = jsonStringify(true);
16+
expect(output).toEqual('true');
2317
});
2418

25-
test('number (Infinity)', () => {
26-
const expectedResult = JSON.stringify(Infinity);
27-
const result = jsonStringify(Infinity);
28-
29-
expect(result).toEqual(expectedResult);
19+
test('42 (number) returns \'42\'', () => {
20+
const output = jsonStringify(42);
21+
expect(output).toEqual('42');
3022
});
3123

32-
test('number (NaN)', () => {
33-
const expectedResult = JSON.stringify(NaN);
34-
const result = jsonStringify(NaN);
35-
36-
expect(result).toEqual(expectedResult);
24+
test('Infinity (number) returns \'null\'', () => {
25+
const output = jsonStringify(Infinity);
26+
expect(output).toEqual('null');
3727
});
3828

39-
test('string', () => {
40-
const expectedResult = JSON.stringify('abc');
41-
const result = jsonStringify('abc');
42-
43-
expect(result).toEqual(expectedResult);
29+
test('NaN (number) returns \'null\'', () => {
30+
const output = jsonStringify(NaN);
31+
expect(output).toEqual('null');
4432
});
4533

46-
test('null', () => {
47-
const expectedResult = JSON.stringify(null);
48-
const result = jsonStringify(null);
49-
50-
expect(result).toEqual(expectedResult);
34+
test('abc (string) returns "abc"', () => {
35+
const output = jsonStringify('abc');
36+
expect(output).toEqual('"abc"');
5137
});
5238

53-
test('buffer', () => {
54-
const buf = Buffer.from('abc');
55-
const expectedResult = JSON.stringify(buf);
56-
const result = jsonStringify(buf);
57-
58-
expect(result).toEqual(expectedResult);
39+
test('a buffer returns the same output as JSON.stringify()', () => {
40+
const value = Buffer.from('abc');
41+
const expectedOutput = JSON.stringify(value);
42+
const output = jsonStringify(value);
43+
expect(output).toEqual(expectedOutput);
5944
});
6045

61-
test('date', () => {
62-
const date = new Date();
63-
const expectedResult = JSON.stringify(date);
64-
const result = jsonStringify(date);
65-
66-
expect(result).toEqual(expectedResult);
46+
test('a date returns the same output as JSON.stringify()', () => {
47+
const value = new Date();
48+
const expectedOutput = JSON.stringify(value);
49+
const output = jsonStringify(value);
50+
expect(output).toEqual(expectedOutput);
6751
});
6852
});
6953

7054
describe('Passing a single BigInt', () => {
7155
test('throws an error', () => {
72-
expect(() => jsonStringify(BigInt(42))).toThrow('Cannot serialize a BigInt');
56+
const value = BigInt(42);
57+
expect(() => jsonStringify(value)).toThrow('Cannot serialize a BigInt');
7358
});
7459
});
7560

76-
describe('Calling JSON.parse() on a stringifed object with all possible values returns the same result as JSON.stringify()', () => {
61+
describe('Output that can be parsed with JSON.parse() is returned when serializing', () => {
7762
const obj = {
7863
a: true,
7964
b: 42,
@@ -84,23 +69,24 @@ describe('Calling JSON.parse() on a stringifed object with all possible values r
8469
g: Infinity,
8570
h: NaN,
8671
i: new Date(),
87-
j: '😀',
8872
k: Symbol(),
8973
l: () => {},
9074
m: new Map(),
9175
n: {
92-
one: 1,
93-
two: 2,
94-
three: 3,
95-
nestedObject: {},
96-
nestedArray: [],
76+
nestedObject: {
77+
a: true,
78+
},
79+
nestedArray: [
80+
true,
81+
],
9782
},
9883
o: [
99-
1,
100-
2,
101-
3,
102-
{},
103-
[],
84+
{
85+
a: true,
86+
},
87+
[
88+
true,
89+
],
10490
],
10591
};
10692

@@ -114,144 +100,103 @@ describe('Calling JSON.parse() on a stringifed object with all possible values r
114100
Infinity,
115101
NaN,
116102
new Date(),
117-
'😀',
118103
Symbol(),
119104
() => {},
120105
new Map(),
121106
{
122-
one: 1,
123-
two: 2,
124-
three: 3,
125-
nestedObject: {},
126-
nestedArray: [],
107+
nestedObject: {
108+
a: true,
109+
},
110+
nestedArray: [
111+
true,
112+
],
127113
},
128114
[
129-
1,
130-
2,
131-
3,
132-
{},
133-
[],
115+
{
116+
a: true,
117+
},
118+
[
119+
true,
120+
],
134121
],
135122
];
136123

137-
test('Standard object', () => {
138-
const expectedResult = JSON.stringify(obj);
139-
const result = jsonStringify(obj);
140-
141-
expect(result).toBeDefined();
142-
143-
if (result !== undefined) {
144-
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
145-
}
124+
test('an object', () => {
125+
const output = jsonStringify(obj);
126+
expect(output).toBeDefined();
127+
expect(() => JSON.parse(output!)).not.toThrowError();
146128
});
147129

148-
test('Array', () => {
149-
const expectedResult = JSON.stringify(arr);
150-
const result = jsonStringify(arr);
151-
152-
expect(result).toBeDefined();
153-
154-
if (result !== undefined) {
155-
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
156-
}
130+
test('an array', () => {
131+
const output = jsonStringify(arr);
132+
expect(output).toBeDefined();
133+
expect(() => JSON.parse(output!)).not.toThrowError();
157134
});
158135

159-
test('Standard object, safe set to false', () => {
160-
const expectedResult = JSON.stringify(obj);
161-
const result = jsonStringify(obj, false);
162-
163-
expect(result).toBeDefined();
164-
165-
if (result !== undefined) {
166-
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
167-
}
136+
test('an object with \'safe\' argument set to false', () => {
137+
const output = jsonStringify(obj, false);
138+
expect(output).toBeDefined();
139+
expect(() => JSON.parse(output!)).not.toThrowError();
168140
});
169141

170-
test('Array, safe set to false', () => {
171-
const expectedResult = JSON.stringify(arr);
172-
const result = jsonStringify(arr, false);
173-
174-
expect(result).toBeDefined();
175-
176-
if (result !== undefined) {
177-
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
178-
}
142+
test('an array with \'safe\' argument set to false', () => {
143+
const output = jsonStringify(arr, false);
144+
expect(output).toBeDefined();
145+
expect(() => JSON.parse(output!)).not.toThrowError();
179146
});
180147
});
181148

182-
describe('Stringifying an object with one string value returns the same result as JSON.stringify() when the string includes', () => {
183-
test('a double quote (")', () => {
184-
const obj = { a: '"abc"' };
185-
186-
const expectedResult = JSON.stringify(obj);
187-
const result = jsonStringify(obj, false);
188-
189-
expect(result).toEqual(expectedResult);
190-
});
191-
149+
describe('Serializing a string value with an escape character returns the correct output', () => {
192150
test('a backslash (\\)', () => {
193-
const obj = { a: 'ab\\c' };
194-
195-
const expectedResult = JSON.stringify(obj);
196-
const result = jsonStringify(obj, false);
197-
198-
expect(result).toEqual(expectedResult);
151+
const output = jsonStringify('ab\\c');
152+
expect(output).toEqual('"ab\\\\c"');
199153
});
200154

201-
test('an emoji', () => {
202-
const obj = { a: '😀' };
203-
204-
const expectedResult = JSON.stringify(obj);
205-
const result = jsonStringify(obj, false);
155+
test('a double quote (")', () => {
156+
const output = jsonStringify('"abc"');
157+
expect(output).toEqual('"\\"abc\\""');
158+
});
206159

207-
expect(result).toEqual(expectedResult);
160+
test('a backslash (\\) and a double quote (")', () => {
161+
const output = jsonStringify('"ab\\c"');
162+
expect(output).toEqual('"\\"ab\\\\c\\""');
208163
});
209164
});
210165

211-
describe('Stringifying an object with a BigInt throws an error', () => {
212-
test('Object', () => {
213-
const obj = { foo: BigInt(42) };
214-
215-
expect(() => jsonStringify(obj, true)).toThrow('Cannot serialize objects that contain a BigInt');
166+
describe('An error is thrown when serializing a BigInt inside', () => {
167+
test('an object', () => {
168+
const value = { foo: BigInt(42) };
169+
expect(() => jsonStringify(value)).toThrow('Cannot serialize objects that contain a BigInt');
216170
});
217171

218-
test('Array', () => {
219-
const arr = [BigInt(42)];
220-
221-
expect(() => jsonStringify(arr, true)).toThrow('Cannot serialize objects that contain a BigInt');
172+
test('an array', () => {
173+
const value = [BigInt(42)];
174+
expect(() => jsonStringify(value)).toThrow('Cannot serialize objects that contain a BigInt');
222175
});
223176
});
224177

225-
describe('Stringifying an object with a circular reference throws an error', () => {
226-
test('Object with \'safe\' argument set to true', () => {
227-
const obj: any = {};
228-
229-
obj.a = obj;
230-
231-
expect(() => jsonStringify(obj, true)).toThrow('Cannot serialize objects that contain a circular reference');
178+
describe('Serializing an object with a circular reference throws an error', () => {
179+
test('object with \'safe\' argument set to true', () => {
180+
const value: any = {};
181+
value.a = value;
182+
expect(() => jsonStringify(value, true)).toThrow('Cannot serialize objects that contain a circular reference');
232183
});
233184

234-
test('Array with \'safe\' argument set to true', () => {
235-
const arr: any[] = [];
236-
237-
arr[0] = arr;
238-
239-
expect(() => jsonStringify(arr, true)).toThrow('Cannot serialize objects that contain a circular reference');
185+
test('array with \'safe\' argument set to true', () => {
186+
const value: any[] = [];
187+
value[0] = value;
188+
expect(() => jsonStringify(value, true)).toThrow('Cannot serialize objects that contain a circular reference');
240189
});
241190

242-
test('Object with \'safe\' argument set to false', () => {
243-
const obj: any = {};
244-
245-
obj.a = obj;
246-
247-
expect(() => jsonStringify(obj, false)).toThrow('Maximum call stack size exceeded');
191+
test('object with \'safe\' argument set to false', () => {
192+
const value: any = {};
193+
value.a = value;
194+
expect(() => jsonStringify(value, false)).toThrow('Maximum call stack size exceeded');
248195
});
249196

250-
test('Array with \'safe\' argument set to false', () => {
251-
const arr: any[] = [];
252-
253-
arr[0] = arr;
254-
255-
expect(() => jsonStringify(arr, false)).toThrow('Maximum call stack size exceeded');
197+
test('array with \'safe\' argument set to false', () => {
198+
const value: any[] = [];
199+
value[0] = value;
200+
expect(() => jsonStringify(value, false)).toThrow('Maximum call stack size exceeded');
256201
});
257202
});

0 commit comments

Comments
 (0)