Skip to content

Commit 43e06cb

Browse files
committed
feat: Keep bars aligned when test names are longer than the requested length.
1 parent 5c5bbb6 commit 43e06cb

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/reporter/chart.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ function chartReport(results, options = { labelWidth: 45, printHeader: true }) {
116116
process.stdout.write(styleText("bold", "\nSummary (vs. baseline):\n"));
117117
}
118118

119+
const maxNameLength = Math.max(...results.map((r) => r.name.length));
120+
const columnWidth = Math.max(maxNameLength, options.labelWidth ?? 45);
121+
119122
for (const result of results) {
120123
let comment = "";
121124

@@ -129,8 +132,6 @@ function chartReport(results, options = { labelWidth: 45, printHeader: true }) {
129132
}
130133
}
131134

132-
const columnWidth = options.labelWidth ?? 45;
133-
134135
drawBar(
135136
result.name.padEnd(columnWidth),
136137
result[primaryMetric],

test/reporter.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,46 @@ describe("chartReport", () => {
7373
});
7474
});
7575

76+
describe("with long names", async (t) => {
77+
let output = "";
78+
79+
before(async () => {
80+
const originalStdoutWrite = process.stdout.write;
81+
process.stdout.write = (data) => {
82+
output += data;
83+
};
84+
85+
const suite = new Suite({
86+
reporter: chartReport,
87+
});
88+
89+
suite
90+
.add("single with matcher looooooooooooooooooooooooooong", () => {
91+
const pattern = /[123]/g;
92+
const replacements = { 1: "a", 2: "b", 3: "c" };
93+
const subject = "123123123123123123123123123123123123123123123123";
94+
const r = subject.replace(pattern, (m) => replacements[m]);
95+
assert.ok(r);
96+
})
97+
.add("multiple replaces", () => {
98+
const subject = "123123123123123123123123123123123123123123123123";
99+
const r = subject
100+
.replace(/1/g, "a")
101+
.replace(/2/g, "b")
102+
.replace(/3/g, "c");
103+
assert.ok(r);
104+
});
105+
await suite.run();
106+
107+
process.stdout.write = originalStdoutWrite;
108+
});
109+
110+
it("should pad out benchmark names", () => {
111+
assert.ok(output.includes("oong | "));
112+
assert.ok(output.includes("multiple replaces".padEnd(51)));
113+
});
114+
});
115+
76116
describe("respects reporterOptions.printHeader", async (t) => {
77117
let outputWithHeader = "";
78118
let outputWithoutHeader = "";

0 commit comments

Comments
 (0)