-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathstatsUtils.js
More file actions
81 lines (72 loc) · 2.13 KB
/
statsUtils.js
File metadata and controls
81 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
81
const path = require("path");
const { readFileSync } = require("fs");
const { globSync } = require("tinyglobby");
const { StatsSerializeStream } = require("../lib/statsUtils");
describe("StatsSerializeStream", () => {
it("should properly stringify primitives", function () {
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", function () {
expectProperJson([]);
expectProperJson([1, undefined, 2]);
// eslint-disable-next-line
expectProperJson([1, , 2]);
expectProperJson([false, "f'o\"o", -1, 42.42]);
});
it("should properly stringify objects", function () {
expectProperJson({});
expectProperJson({
a: 1,
"foo-bar": null,
undef: undefined,
'"Гусь!"': true,
});
});
it("should properly stringify complex structures", function () {
expectProperJson({
foo: [],
bar: {
baz: [
1,
{ a: 1, b: ["foo", "bar"], c: [] },
"foo",
{ a: 1, b: undefined, c: [{ d: true }] },
null,
undefined,
],
},
});
});
globSync("stats/**/*.json", { cwd: __dirname }).forEach((filepath) => {
it(`should properly stringify JSON from "${filepath}"`, function () {
const content = readFileSync(path.resolve(__dirname, filepath), "utf8");
const json = JSON.parse(content);
expectProperJson(json);
});
});
});
async function expectProperJson(json) {
expect(await stringify(json)).toBe(JSON.stringify(json, null, 2));
}
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);
});
}