sumlyzer 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sumlyzer",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Run every npm workspace's test script, only show output for the ones that fail, and finish with an aggregated pass/fail summary.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -39,7 +39,10 @@
39
39
  "node-test",
40
40
  "node:test",
41
41
  "test-summary",
42
- "ci"
42
+ "ci",
43
+ "junit",
44
+ "junit-xml",
45
+ "github-actions"
43
46
  ],
44
47
  "author": {
45
48
  "name": "Erwan Raulo",
@@ -0,0 +1,41 @@
1
+ const COUNT_INDEX = 1;
2
+ const count = (target, output) => new RegExp(String.raw`ℹ ${target} (\d+)`).exec(output)?.[COUNT_INDEX];
3
+
4
+ export function parseTestCounts(output) {
5
+ const tests = count("tests", output);
6
+
7
+ if (!tests) {
8
+ return null;
9
+ }
10
+
11
+ return {
12
+ tests,
13
+ pass: count("pass", output) ?? "?",
14
+ fail: count("fail", output) ?? "?",
15
+ durationMs: count("duration_ms", output)
16
+ };
17
+ }
18
+
19
+ export function parseFailingTests(output) {
20
+ const [, section] = output.split("✖ failing tests:");
21
+ if (!section) {
22
+ return [];
23
+ }
24
+
25
+ return [...section.matchAll(/^✖ (.+) \([\d.]+ms\)$/gm)].map((match) => match[1]);
26
+ }
27
+
28
+ export function stripNpmNoise(output) {
29
+ return output.replace(/^npm warn .*\n?/gm, "");
30
+ }
31
+
32
+ export function extractFailureDetails(output) {
33
+ const [, section] = output.split("✖ failing tests:");
34
+ if (!section) {
35
+ return output.trim();
36
+ }
37
+
38
+ const details = section.split(/\n(?=npm )/)[0].trim();
39
+
40
+ return details.includes("'test failed'") ? output.trim() : details;
41
+ }
@@ -0,0 +1,108 @@
1
+ import { styleText } from "node:util";
2
+
3
+ export const green = (str) => styleText("green", str);
4
+ export const red = (str) => styleText("red", str);
5
+ export const dim = (str) => styleText("dim", str);
6
+ export const bold = (str) => styleText("bold", str);
7
+
8
+ export function formatSeconds(ms) {
9
+ const seconds = Number.isFinite(ms) ? ms / 1000 : 0;
10
+ return `${seconds.toFixed(1)}s`;
11
+ }
12
+
13
+ // GitHub Actions: https://docs.github.com/actions/how-tos/write-workflows/choose-what-workflows-do/workflow-commands#grouping-log-lines
14
+ export function githubGroupSyntax(env) {
15
+ if (env.GITHUB_ACTIONS === "true") {
16
+ return {
17
+ start: (title) => `::group::${title}`,
18
+ end: () => "::endgroup::"
19
+ };
20
+ }
21
+ return null;
22
+ }
23
+
24
+ export function workspaceName(wsPath) {
25
+ return (wsPath ?? "").replace(/^workspaces[\\/]/, "");
26
+ }
27
+
28
+ export function ciGroupTitle(name, result) {
29
+ const seconds = formatSeconds(result.durationMs);
30
+ const countsLabel = result.counts ? ` (${result.counts.pass}/${result.counts.tests} tests)` : "";
31
+ const icon = result.exitCode === 0 ? "✓" : "✗";
32
+ return `${icon} ${name} ${seconds}${countsLabel}`;
33
+ }
34
+
35
+ function printWorkspaceResultCi(name, result, ciGroup) {
36
+ process.stdout.write(`${ciGroup.start(ciGroupTitle(name, result))}\n`);
37
+ process.stdout.write(`${result.rawOutput || "(no output captured)"}\n`);
38
+ process.stdout.write(`${ciGroup.end()}\n`);
39
+ }
40
+
41
+ export function printWorkspaceResult(name, result, ciGroup) {
42
+ if (ciGroup) {
43
+ printWorkspaceResultCi(name, result, ciGroup);
44
+ return;
45
+ }
46
+
47
+ const seconds = formatSeconds(result.durationMs);
48
+ const countsLabel = result.counts ? ` (${result.counts.pass}/${result.counts.tests} tests)` : "";
49
+
50
+ if (result.exitCode === 0) {
51
+ process.stdout.write(green(`✓ ${name}`) + dim(` ${seconds}${countsLabel}\n`));
52
+ return;
53
+ }
54
+
55
+ process.stdout.write(`\n${bold(red(`✗ ${name} failed`))}\n`);
56
+ process.stdout.write(result.failureDetails + "\n");
57
+ process.stdout.write(red(`✗ ${name}`) + dim(` ${seconds}, exit code ${result.exitCode}\n\n`));
58
+ }
59
+
60
+ function summaryRow(workspace, status, duration, counts) {
61
+ return {
62
+ workspace,
63
+ status,
64
+ duration,
65
+ testsDuration: counts?.durationMs ? formatSeconds(Number(counts.durationMs)) : "-",
66
+ tests: counts?.tests ?? "-",
67
+ pass: counts?.pass ?? "-",
68
+ fail: counts?.fail ?? "-"
69
+ };
70
+ }
71
+
72
+ export function printSummary(results, skipped) {
73
+ console.log(bold("\nSummary"));
74
+ console.table([
75
+ ...results.map(({ wsPath, exitCode, durationMs, counts }) => {
76
+
77
+ return summaryRow(
78
+ workspaceName(wsPath),
79
+ exitCode === 0 ? "PASS" : "FAIL",
80
+ formatSeconds(durationMs),
81
+ counts
82
+ );
83
+ }),
84
+ ...skipped.map((wsPath) => summaryRow(workspaceName(wsPath), "SKIPPED", "-", null))
85
+ ]);
86
+
87
+ if (skipped.length > 0) {
88
+ console.log(dim(`${skipped.length} workspace(s) skipped (--ff): ${skipped.map(workspaceName).join(", ")}`));
89
+ }
90
+ }
91
+
92
+ export function reportOutcome(results) {
93
+ const failed = results.filter((result) => result.exitCode !== 0);
94
+
95
+ if (failed.length === 0) {
96
+ console.log(green(`${results.length}/${results.length} workspaces passed.`));
97
+ return;
98
+ }
99
+
100
+ console.log(red(`${failed.length}/${results.length} workspace(s) failed:`));
101
+ for (const result of failed) {
102
+ console.log(red(` ${workspaceName(result.wsPath)}`));
103
+ for (const testName of result.failingTests) {
104
+ console.log(dim(` ✖ ${testName}`));
105
+ }
106
+ }
107
+ process.exitCode = 1;
108
+ }
package/src/run.mjs CHANGED
@@ -2,29 +2,21 @@ import { execFile } from "node:child_process";
2
2
  import { readFileSync } from "node:fs";
3
3
  import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
4
4
  import { tmpdir } from "node:os";
5
- import { promisify, styleText } from "node:util";
5
+ import { promisify } from "node:util";
6
6
  import path from "node:path";
7
7
 
8
8
  import { buildJunitReport } from "./junit.mjs";
9
+ import { parseTestCounts, parseFailingTests, stripNpmNoise, extractFailureDetails } from "./parseOutput.mjs";
10
+ import { red, dim, workspaceName, githubGroupSyntax, printWorkspaceResult, printSummary, reportOutcome } from "./reporter.mjs";
9
11
 
10
12
  const execFileAsync = promisify(execFile);
11
13
 
12
- const green = (str) => styleText("green", str);
13
- const red = (str) => styleText("red", str);
14
- const dim = (str) => styleText("dim", str);
15
- const bold = (str) => styleText("bold", str);
16
-
17
- const COUNT_INDEX = 1;
18
- const count = (target, output) => new RegExp(String.raw`ℹ ${target} (\d+)`).exec(output)?.[COUNT_INDEX];
19
-
20
14
  const SPEC_REPORTER_FLAGS = "--test-reporter=spec --test-reporter-destination=stdout";
21
15
 
22
16
  export function envWithSpecReporter(env, junitDestPath) {
23
17
  const existing = env.NODE_OPTIONS ? `${env.NODE_OPTIONS} ` : "";
24
18
  const junitFlags = junitDestPath ? ` --test-reporter=junit --test-reporter-destination=${junitDestPath}` : "";
25
- // Drop NODE_TEST_CONTEXT: if sumlyzer itself is invoked from inside a node:test run (e.g.
26
- // its own test suite), this var would otherwise leak into the workspace's own `node --test`
27
- // process, which then silently treats itself as a nested child and stops reporting normally.
19
+ // Drop NODE_TEST_CONTEXT: if sumlyzer itself is invoked from inside a node:test run, this var would otherwise leak.
28
20
  const rest = { ...env };
29
21
  delete rest.NODE_TEST_CONTEXT;
30
22
  return { ...rest, NODE_OPTIONS: `${existing}${SPEC_REPORTER_FLAGS}${junitFlags}` };
@@ -34,10 +26,6 @@ function readJson(file) {
34
26
  return JSON.parse(readFileSync(file, "utf8"));
35
27
  }
36
28
 
37
- export function workspaceName(wsPath) {
38
- return (wsPath ?? "").replace(/^workspaces[\\/]/, "");
39
- }
40
-
41
29
  function listEligibleWorkspaces(root, workspaces, scriptName) {
42
30
  return workspaces.filter((wsPath) => {
43
31
  const pkgFile = path.join(root, wsPath, "package.json");
@@ -50,50 +38,6 @@ function listEligibleWorkspaces(root, workspaces, scriptName) {
50
38
  });
51
39
  }
52
40
 
53
- export function parseTestCounts(output) {
54
- const tests = count("tests", output);
55
-
56
- if (!tests) {
57
- return null;
58
- }
59
-
60
- return {
61
- tests,
62
- pass: count("pass", output) ?? "?",
63
- fail: count("fail", output) ?? "?",
64
- durationMs: count("duration_ms", output)
65
- };
66
- }
67
-
68
- export function parseFailingTests(output) {
69
- const [, section] = output.split("✖ failing tests:");
70
- if (!section) {
71
- return [];
72
- }
73
-
74
- return [...section.matchAll(/^✖ (.+) \([\d.]+ms\)$/gm)].map((match) => match[1]);
75
- }
76
-
77
- export function stripNpmNoise(output) {
78
- return output.replace(/^npm warn .*\n?/gm, "");
79
- }
80
-
81
- export function extractFailureDetails(output) {
82
- const [, section] = output.split("✖ failing tests:");
83
- if (!section) {
84
- return output.trim();
85
- }
86
-
87
- const details = section.split(/\n(?=npm )/)[0].trim();
88
-
89
- return details.includes("'test failed'") ? output.trim() : details;
90
- }
91
-
92
- export function formatSeconds(ms) {
93
- const seconds = Number.isFinite(ms) ? ms / 1000 : 0;
94
- return `${seconds.toFixed(1)}s`;
95
- }
96
-
97
41
  async function runWorkspaceScript(root, wsPath, scriptName, junitDestPath) {
98
42
  const start = Date.now();
99
43
  const options = {
@@ -129,30 +73,6 @@ async function runWorkspaceScript(root, wsPath, scriptName, junitDestPath) {
129
73
  };
130
74
  }
131
75
 
132
- // GitHub Actions: https://docs.github.com/actions/how-tos/write-workflows/choose-what-workflows-do/workflow-commands#grouping-log-lines
133
- export function ciGroupSyntax(env) {
134
- if (env.GITHUB_ACTIONS === "true") {
135
- return {
136
- start: (title) => `::group::${title}`,
137
- end: () => "::endgroup::"
138
- };
139
- }
140
- return null;
141
- }
142
-
143
- export function ciGroupTitle(name, result) {
144
- const seconds = formatSeconds(result.durationMs);
145
- const countsLabel = result.counts ? ` (${result.counts.pass}/${result.counts.tests} tests)` : "";
146
- const icon = result.exitCode === 0 ? "✓" : "✗";
147
- return `${icon} ${name} ${seconds}${countsLabel}`;
148
- }
149
-
150
- function printWorkspaceResultCi(name, result, ciGroup) {
151
- process.stdout.write(`${ciGroup.start(ciGroupTitle(name, result))}\n`);
152
- process.stdout.write(`${result.rawOutput || "(no output captured)"}\n`);
153
- process.stdout.write(`${ciGroup.end()}\n`);
154
- }
155
-
156
76
  async function collectJunitEntries(results) {
157
77
  const entries = [];
158
78
  const missing = [];
@@ -165,9 +85,6 @@ async function collectJunitEntries(results) {
165
85
  entries.push({ name: workspaceName(result.wsPath), xml: await readFile(result.junitDestPath, "utf8") });
166
86
  }
167
87
  catch {
168
- // Either the workspace crashed before node:test's own junit reporter could write its
169
- // file, or --script points at something that isn't node:test at all; skip it, but the
170
- // caller still needs to know so it can tell the user the report is missing an entry.
171
88
  missing.push(workspaceName(result.wsPath));
172
89
  }
173
90
  }
@@ -175,35 +92,9 @@ async function collectJunitEntries(results) {
175
92
  return { entries, missing };
176
93
  }
177
94
 
178
- function summaryRow(workspace, status, duration, counts) {
179
- return {
180
- workspace,
181
- status,
182
- duration,
183
- testsDuration: counts?.durationMs ? formatSeconds(Number(counts.durationMs)) : "-",
184
- tests: counts?.tests ?? "-",
185
- pass: counts?.pass ?? "-",
186
- fail: counts?.fail ?? "-"
187
- };
188
- }
189
-
190
- function printWorkspaceResult(name, result) {
191
- const seconds = formatSeconds(result.durationMs);
192
- const countsLabel = result.counts ? ` (${result.counts.pass}/${result.counts.tests} tests)` : "";
193
-
194
- if (result.exitCode === 0) {
195
- process.stdout.write(green(`✓ ${name}`) + dim(` ${seconds}${countsLabel}\n`));
196
- return;
197
- }
198
-
199
- process.stdout.write(`\n${bold(red(`✗ ${name} failed`))}\n`);
200
- process.stdout.write(result.failureDetails + "\n");
201
- process.stdout.write(red(`✗ ${name}`) + dim(` ${seconds}, exit code ${result.exitCode}\n\n`));
202
- }
203
-
204
95
  async function runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir }) {
205
96
  const results = [];
206
- const ciGroup = ciGroupSyntax(process.env);
97
+ const ciGroup = githubGroupSyntax(process.env);
207
98
 
208
99
  for (const [index, wsPath] of workspacesToRun.entries()) {
209
100
  const name = workspaceName(wsPath);
@@ -220,13 +111,7 @@ async function runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir }
220
111
  process.exit(1);
221
112
  }
222
113
  results.push(result);
223
-
224
- if (ciGroup) {
225
- printWorkspaceResultCi(name, result, ciGroup);
226
- }
227
- else {
228
- printWorkspaceResult(name, result);
229
- }
114
+ printWorkspaceResult(name, result, ciGroup);
230
115
 
231
116
  if (result.exitCode !== 0 && ff) {
232
117
  break;
@@ -262,41 +147,6 @@ async function writeJunitReport(root, junitPath, results) {
262
147
  }
263
148
  }
264
149
 
265
- function printSummary(results, skipped) {
266
- console.log(bold("\nSummary"));
267
- console.table([
268
- ...results.map((result) => summaryRow(
269
- workspaceName(result.wsPath),
270
- result.exitCode === 0 ? "PASS" : "FAIL",
271
- formatSeconds(result.durationMs),
272
- result.counts
273
- )),
274
- ...skipped.map((wsPath) => summaryRow(workspaceName(wsPath), "SKIPPED", "-", null))
275
- ]);
276
-
277
- if (skipped.length > 0) {
278
- console.log(dim(`${skipped.length} workspace(s) skipped (--ff): ${skipped.map(workspaceName).join(", ")}`));
279
- }
280
- }
281
-
282
- function reportOutcome(results) {
283
- const failed = results.filter((result) => result.exitCode !== 0);
284
-
285
- if (failed.length === 0) {
286
- console.log(green(`${results.length}/${results.length} workspaces passed.`));
287
- return;
288
- }
289
-
290
- console.log(red(`${failed.length}/${results.length} workspace(s) failed:`));
291
- for (const result of failed) {
292
- console.log(red(` ${workspaceName(result.wsPath)}`));
293
- for (const testName of result.failingTests) {
294
- console.log(dim(` ✖ ${testName}`));
295
- }
296
- }
297
- process.exitCode = 1;
298
- }
299
-
300
150
  export async function main({ root, scriptName, ff, junitPath }) {
301
151
 
302
152
  const { workspaces } = readJson(path.join(root, "package.json"));