-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathstatsUtils.js
More file actions
80 lines (72 loc) · 2.13 KB
/
statsUtils.js
File metadata and controls
80 lines (72 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { readFileSync } = require("node:fs");
const path = require("node:path");
const { globSync } = require("tinyglobby");
const { StatsSerializeStream } = require("../src/statsUtils");
async function stringify(json) {
return new Promise((resolve, reject) => {
let result = "";
new StatsSerializeStream(json)
.on("data", (chunk) => (result += chunk))
.on("end", () => resolve(result))
.on("error", reject);
});
}
async function expectProperJson(json) {
expect(await stringify(json)).toBe(JSON.stringify(json, null, 2));
}
describe("StatsSerializeStream", () => {
it("should properly stringify primitives", () => {
expectProperJson(0);
expectProperJson(1);
expectProperJson(-1);
expectProperJson(42.42);
expectProperJson(-42.42);
expectProperJson(false);
expectProperJson(true);
expectProperJson(null);
expectProperJson(null);
expectProperJson("");
expectProperJson('"');
expectProperJson("foo bar");
expectProperJson('"foo bar"');
expectProperJson("Вива Лас-Вегас!");
});
it("should properly stringify simple arrays", () => {
expectProperJson([]);
expectProperJson([1, undefined, 2]);
// eslint-disable-next-line no-sparse-arrays
expectProperJson([1, , 2]);
expectProperJson([false, "f'o\"o", -1, 42.42]);
});
it("should properly stringify objects", () => {
expectProperJson({});
expectProperJson({
a: 1,
"foo-bar": null,
undef: undefined,
'"Гусь!"': true,
});
});
it("should properly stringify complex structures", () => {
expectProperJson({
foo: [],
bar: {
baz: [
1,
{ a: 1, b: ["foo", "bar"], c: [] },
"foo",
{ a: 1, b: undefined, c: [{ d: true }] },
null,
undefined,
],
},
});
});
for (const filepath of globSync("stats/**/*.json", { cwd: __dirname })) {
it(`should properly stringify JSON from "${filepath}"`, () => {
const content = readFileSync(path.resolve(__dirname, filepath), "utf8");
const json = JSON.parse(content);
expectProperJson(json);
});
}
});