-
Notifications
You must be signed in to change notification settings - Fork 248
/
format-helpers.spec.mjs
101 lines (84 loc) · 3.55 KB
/
format-helpers.spec.mjs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable no-magic-numbers */
import { expect } from "chai";
import chalk from "chalk";
import formatHelpers from "../../../../src/cli/listeners/performance-log/format-helpers.js";
/*
* as the formatHelpers use the Intl API, it's necessary to set the locale
* to something uniform so we can actually work with expectations.
*
* The expected values are for now based on the en-US locale - as it's likely
* to be available on most systems. To ensure the formatHelpers are also on
* that set the LANG environment variable to en_US.UTF-8. E.g. to run these
* tests:
*
* LANG=en_US.UTF-8 mocha
*
* (the run-scripts in dependency-cruiser's package.json have all been adjusted
* thusly)
*/
describe("[U] cli/listeners/performance-log/format-helpers - formatTime", () => {
it("converts to ms, left pads & adds the unit at the end", () => {
expect(formatHelpers.formatTime(14.88041018)).to.equal(" 14,880ms ");
});
it("converts to ms, left pads & adds the unit at the end (0)", () => {
expect(formatHelpers.formatTime(0)).to.equal(" 0ms ");
});
it("converts to ms, left pads & adds the unit at the end (negative numbers)", () => {
expect(formatHelpers.formatTime(-3.1415926535)).to.equal(" -3,142ms ");
});
it("converts to ms, left pads & adds the unit at the end (null treatment => 0)", () => {
expect(formatHelpers.formatTime(null)).to.equal(" 0ms ");
});
it("converts to ms, left pads & adds the unit at the end (undefined treatment => NaN)", () => {
expect(formatHelpers.formatTime()).to.equal(" NaNms ");
});
it("converts to ms, left pads & adds the unit at the end (non-number treatment => NaN)", () => {
expect(formatHelpers.formatTime("not a number")).to.equal(" NaNms ");
});
});
describe("[U] cli/listeners/performance-log/format-helpers - formatMemory", () => {
let lChalkLevel = chalk.level;
before("disable chalk coloring", () => {
chalk.level = 0;
});
after("enable chalk coloring again", () => {
chalk.level = lChalkLevel;
});
it("converts to kB, left pads & adds the unit at the end", () => {
expect(formatHelpers.formatMemory(4033856)).to.equal(" +3,939kB ");
});
it("converts to kB, left pads & adds the unit at the end (0)", () => {
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB ");
});
it("converts to kB, left pads & adds the unit at the end (negative numbers)", () => {
expect(formatHelpers.formatMemory(-403385623)).to.equal(" -393,931kB ");
});
it("converts to kB, left pads & adds the unit at the end (null)", () => {
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB ");
});
it("converts to kB, left pads & adds the unit at the end (undefined)", () => {
expect(formatHelpers.formatMemory()).to.equal(" NaNkB ");
});
it("converts to kB, left pads & adds the unit at the end (not a number)", () => {
expect(formatHelpers.formatMemory("not a number")).to.equal(
" NaNkB "
);
});
});
describe("[U] cli/listeners/performance-log/format-helpers - formatPerfLine", () => {
it("produces neat columns with time, memory and a message", () => {
const lStats = {
elapsedTime: 7.634973442,
elapsedUser: 16497,
elapsedSystem: 3518,
deltaRss: 3219456,
deltaHeapUsed: 2857936,
deltaHeapTotal: 2478080,
deltaExternal: 0,
message: "sim sala bim",
};
expect(formatHelpers.formatPerfLine(lStats, "sim sala bim")).to.match(
/ {0,}7,635ms {0,}16ms {0,}4ms {0,}\+3,144kB {0,}\+2,420kB {0,}\+2,791kB {0,}0kB sim sala bim\n/
);
});
});