sumlyzer 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -85,31 +85,7 @@ This also applies when `--junit` can't write its report.
85
85
 
86
86
  ## Example
87
87
 
88
- ```
89
- running contact
90
- ✓ contact 2.6s (35/35 tests)
91
- running scanner
92
- ✓ scanner 10.1s (161/162 tests)
93
- running tarball
94
- ✓ tarball 1.8s (77/77 tests)
95
- running tree-walker
96
- ✓ tree-walker 3.0s (26/26 tests)
97
- running flags
98
- ✓ flags 1.1s (9/9 tests)
99
-
100
- Summary
101
- ┌─────────────┬────────┬──────────┬───────────────┬───────┬───────┬──────┬──────┬──────┬───────────┐
102
- │ (index) │ status │ duration │ testsDuration │ tests │ pass │ fail │ skip │ todo │ cancelled │
103
- ├─────────────┼────────┼──────────┼───────────────┼───────┼───────┼──────┼──────┼──────┼───────────┤
104
- │ contact │ 'PASS' │ '2.6s' │ '0.9s' │ '35' │ '35' │ '0' │ '0' │ '0' │ '0' │
105
- │ scanner │ 'PASS' │ '10.1s' │ '8.7s' │ '162' │ '161' │ '0' │ '1' │ '0' │ '0' │
106
- │ tarball │ 'PASS' │ '1.8s' │ '0.7s' │ '77' │ '77' │ '0' │ '0' │ '0' │ '0' │
107
- │ tree-walker │ 'PASS' │ '3.0s' │ '2.0s' │ '26' │ '26' │ '0' │ '0' │ '0' │ '0' │
108
- │ flags │ 'PASS' │ '1.1s' │ '0.1s' │ '9' │ '9' │ '0' │ '0' │ '0' │ '0' │
109
- └─────────────┴────────┴──────────┴───────────────┴───────┴───────┴──────┴──────┴──────┴───────────┘
110
-
111
- 5/5 workspaces passed.
112
- ```
88
+ ![fail fast example](https://github.com/ErwanRaulo/sumlyzer/blob/main/example.png?raw=true)
113
89
 
114
90
  ### Summary columns
115
91
 
@@ -127,46 +103,6 @@ Summary
127
103
  | `cancelled` | Tests reported as cancelled (`ℹ cancelled`), unlike `skip`/`todo` this usually signals a real problem |
128
104
 
129
105
 
130
- ### On failure
131
-
132
- Only the failing workspace prints its detail and passing ones stay collapsed to a single line.
133
-
134
- ```
135
- running contact
136
- ✓ contact 2.6s (35/35 tests)
137
- running scanner
138
- ✓ scanner 10.1s (161/162 tests)
139
- running utils
140
-
141
- ✗ utils failed
142
- test at test/format.spec.mjs:12:3
143
- ✖ formats negative numbers (3ms)
144
- AssertionError [ERR_ASSERTION]: Expected values to be strictly equal
145
- + actual - expected
146
-
147
- + '-1'
148
- - '(1)'
149
-
150
- ✗ utils 1.3s, exit code 1
151
-
152
- running flags
153
- ✓ flags 1.1s (9/9 tests)
154
-
155
- Summary
156
- ┌─────────┬────────┬──────────┬───────────────┬───────┬───────┬──────┬──────┬──────┬───────────┐
157
- │ (index) │ status │ duration │ testsDuration │ tests │ pass │ fail │ skip │ todo │ cancelled │
158
- ├─────────┼────────┼──────────┼───────────────┼───────┼───────┼──────┼──────┼──────┼───────────┤
159
- │ contact │ 'PASS' │ '2.6s' │ '0.9s' │ '35' │ '35' │ '0' │ '0' │ '0' │ '0' │
160
- │ scanner │ 'PASS' │ '10.1s' │ '8.7s' │ '162' │ '161' │ '0' │ '1' │ '0' │ '0' │
161
- │ utils │ 'FAIL' │ '1.3s' │ '0.1s' │ '30' │ '29' │ '1' │ '0' │ '0' │ '0' │
162
- │ flags │ 'PASS' │ '1.1s' │ '0.1s' │ '9' │ '9' │ '0' │ '0' │ '0' │ '0' │
163
- └─────────┴────────┴──────────┴───────────────┴───────┴───────┴──────┴──────┴──────┴───────────┘
164
-
165
- 1/4 workspace(s) failed:
166
- utils
167
- ✖ formats negative numbers
168
- ```
169
-
170
106
  ### JUnit report
171
107
 
172
108
  `--junit <path>` writes a single aggregated JUnit XML report to `<path>`, merging
package/bin/sumlyzer.mjs CHANGED
@@ -5,6 +5,11 @@ import { availableParallelism } from "node:os";
5
5
 
6
6
  import { main } from "../src/run.mjs";
7
7
  import { NoWorkspacesError, InvalidPackageJsonError, WorkspaceLaunchError } from "../src/errors.mjs";
8
+ import { red } from "../src/reporter.mjs";
9
+
10
+ function printWorkspaceError(message) {
11
+ console.error(red(message.split("\n").map((line) => `✗ ${line}`).join("\n")));
12
+ }
8
13
 
9
14
  let args;
10
15
  try {
@@ -68,7 +73,7 @@ catch (error) {
68
73
  console.info(error.message);
69
74
  }
70
75
  else if (error instanceof InvalidPackageJsonError || error instanceof WorkspaceLaunchError) {
71
- console.error(error.message);
76
+ printWorkspaceError(error.message);
72
77
  }
73
78
  else {
74
79
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sumlyzer",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
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": {
@@ -56,5 +56,8 @@
56
56
  "@types/node": "^26.1.1",
57
57
  "c8": "^12.0.0",
58
58
  "fast-xml-parser": "^5.10.1"
59
+ },
60
+ "dependencies": {
61
+ "glob": "^13.0.6"
59
62
  }
60
63
  }
package/src/errors.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { red, workspaceName } from "./reporter.mjs";
1
+ import { workspaceName } from "./reporter.mjs";
2
2
 
3
3
  export class NoWorkspacesError extends Error {
4
4
  constructor() {
@@ -9,7 +9,7 @@ export class NoWorkspacesError extends Error {
9
9
 
10
10
  export class InvalidPackageJsonError extends Error {
11
11
  constructor(entries) {
12
- super(entries.map(({ wsPath, message }) => red(`✗ ${workspaceName(wsPath)}: could not read its package.json (${message})`)).join("\n"));
12
+ super(entries.map(({ wsPath, message }) => `${workspaceName(wsPath)}: could not read its package.json (${message})`).join("\n"));
13
13
  this.name = "InvalidPackageJsonError";
14
14
  this.entries = entries;
15
15
  }
@@ -17,7 +17,7 @@ export class InvalidPackageJsonError extends Error {
17
17
 
18
18
  export class WorkspaceLaunchError extends Error {
19
19
  constructor(wsPath, scriptName, cause) {
20
- super(red(`✗ ${workspaceName(wsPath)}: could not launch "${scriptName}" (${cause.message})`), { cause });
20
+ super(`${workspaceName(wsPath)}: could not launch "${scriptName}" (${cause.message})`, { cause });
21
21
  this.name = "WorkspaceLaunchError";
22
22
  this.wsPath = wsPath;
23
23
  this.scriptName = scriptName;
package/src/run.mjs CHANGED
@@ -2,37 +2,58 @@ import { spawn, spawnSync } 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 { fileURLToPath } from "node:url";
6
5
  import path from "node:path";
7
6
 
7
+ import { glob } from "glob";
8
+
8
9
  import { buildJunitReport } from "./junit.mjs";
9
- import { stripNpmNoise } from "./parseOutput.mjs";
10
- import { parseReporterEvents, renderFailureRecap, renderSummaryText } from "./testEventReporter.mjs";
11
10
  import { red, dim, workspaceName, githubGroupSyntax, printWorkspaceResult, printSummary, reportOutcome } from "./reporter.mjs";
12
11
  import { NoWorkspacesError, InvalidPackageJsonError, WorkspaceLaunchError } from "./errors.mjs";
12
+ import { hasOwnReporter, prepareEnv, parseResult } from "./runners/nodeTest.mjs";
13
13
 
14
- const TEST_EVENT_REPORTER_PATH = fileURLToPath(new URL("./testEventReporter.mjs", import.meta.url));
15
-
16
- export function envWithTestReporter(env, junitDestPath) {
17
- const existing = env.NODE_OPTIONS ? `${env.NODE_OPTIONS} ` : "";
18
- const testReporterFlags = `--test-reporter=${TEST_EVENT_REPORTER_PATH} --test-reporter-destination=stdout`;
19
- const junitFlags = junitDestPath ? ` --test-reporter=junit --test-reporter-destination=${junitDestPath}` : "";
20
- // Drop NODE_TEST_CONTEXT: if sumlyzer itself is invoked from inside a node:test run, this var would otherwise leak.
21
- const rest = { ...env };
22
- delete rest.NODE_TEST_CONTEXT;
23
- return { ...rest, NODE_OPTIONS: `${existing}${testReporterFlags}${junitFlags}` };
24
- }
14
+ const runner = { hasOwnReporter, prepareEnv, parseResult };
25
15
 
26
- // Rethink this synchronous approach in case of very large package.json files, or huge amount of workspaces,
16
+ // Rethink this synchronous approach in case of very large package.json files, or huge amount of workspaces,
27
17
  // but for now it's simpler than async and should be fine in practice.
28
18
  function readJson(file) {
29
19
  return JSON.parse(readFileSync(file, "utf8"));
30
20
  }
31
21
 
32
- const OWN_TEST_REPORTER_FLAG = /--test-reporter(?!-destination)\b/;
22
+ // Splits "workspaces" entries into directory-only glob patterns to include and
23
+ // (from any leading "!") patterns to exclude, matching npm's own interpretation
24
+ // of the field.
25
+ function splitWorkspacePatterns(workspaces) {
26
+ const patterns = [];
27
+ const ignore = ["**/node_modules/**"];
28
+
29
+ for (const entry of workspaces) {
30
+ if (entry.startsWith("!")) {
31
+ ignore.push(entry.slice(1));
32
+ }
33
+ else {
34
+ patterns.push(entry.endsWith("/") ? entry : `${entry}/`);
35
+ }
36
+ }
37
+
38
+ return { patterns, ignore };
39
+ }
40
+
41
+ // The "workspaces" field holds glob patterns (e.g. "packages/*"), not necessarily
42
+ // concrete paths. Resolve it the same way npm itself does: glob each pattern
43
+ // against directories only, in declaration order.
44
+ async function resolveWorkspacePaths(root, workspaces) {
45
+ const { patterns, ignore } = splitWorkspacePatterns(workspaces);
46
+ const resolved = new Set();
47
+
48
+ for (const pattern of patterns) {
49
+ const matches = await glob(pattern, { cwd: root, ignore });
50
+
51
+ for (const match of matches.sort((a, b) => a.localeCompare(b, "en"))) {
52
+ resolved.add(match);
53
+ }
54
+ }
33
55
 
34
- export function hasOwnTestReporter(scriptCommand) {
35
- return OWN_TEST_REPORTER_FLAG.test(scriptCommand);
56
+ return [...resolved];
36
57
  }
37
58
 
38
59
  function listEligibleWorkspaces(root, workspaces, scriptName) {
@@ -58,7 +79,7 @@ function listEligibleWorkspaces(root, workspaces, scriptName) {
58
79
  if (!script) {
59
80
  continue;
60
81
  }
61
- if (hasOwnTestReporter(script)) {
82
+ if (runner.hasOwnReporter(script)) {
62
83
  ownReporterConflicts.push(wsPath);
63
84
  }
64
85
  else {
@@ -86,12 +107,12 @@ function killWorkspaceChild(child) {
86
107
  }
87
108
  }
88
109
 
89
- async function captureWorkspaceOutput(root, wsPath, scriptName, junitDestPath, activeChildren) {
110
+ async function captureWorkspaceOutput({ root, wsPath, scriptName, junitDestPath, activeChildren }) {
90
111
  const options = {
91
112
  cwd: root,
92
113
  shell: process.platform === "win32",
93
114
  detached: process.platform !== "win32",
94
- env: envWithTestReporter(process.env, junitDestPath)
115
+ env: runner.prepareEnv(process.env, junitDestPath)
95
116
  };
96
117
 
97
118
  const child = spawn("npm", ["run", scriptName, "--workspace=" + wsPath], options);
@@ -119,32 +140,43 @@ async function captureWorkspaceOutput(root, wsPath, scriptName, junitDestPath, a
119
140
  }
120
141
  }
121
142
 
122
- function buildWorkspaceResult(wsPath, junitDestPath, durationMs, { exitCode, stdout, stderr }) {
123
- const { counts, failingTests, failures } = parseReporterEvents(stdout);
143
+ function killActiveChildren(activeChildren) {
144
+ for (const child of activeChildren) {
145
+ killWorkspaceChild(child);
146
+ }
147
+ }
124
148
 
125
- // Workspaces that don't run through node:test never emit our JSON events; fall
126
- // back to their raw (npm-noise-stripped) output so failures are still visible.
127
- const fallbackOutput = stripNpmNoise(stdout + stderr).trim();
128
- const failureRecap = failures.length > 0 ? renderFailureRecap(failures) : null;
149
+ async function withInterruptHandling(activeChildren, run) {
150
+ function onInterrupt(signal) {
151
+ killActiveChildren(activeChildren);
152
+ process.exit(signal === "SIGINT" ? 130 : 143);
153
+ }
154
+
155
+ process.once("SIGINT", onInterrupt);
156
+ process.once("SIGTERM", onInterrupt);
157
+
158
+ try {
159
+ return await run();
160
+ }
161
+ finally {
162
+ process.off("SIGINT", onInterrupt);
163
+ process.off("SIGTERM", onInterrupt);
164
+ }
165
+ }
166
+
167
+ async function runWorkspaceScript({ root, wsPath, scriptName, junitDestPath, activeChildren }) {
168
+ const start = Date.now();
169
+ const captured = await captureWorkspaceOutput({ root, wsPath, scriptName, junitDestPath, activeChildren });
129
170
 
130
171
  return {
131
172
  wsPath,
132
- exitCode,
133
- failureDetails: exitCode === 0 ? null : (failureRecap ?? fallbackOutput),
134
- durationMs,
135
- counts,
136
- failingTests,
173
+ exitCode: captured.exitCode,
174
+ durationMs: Date.now() - start,
137
175
  junitDestPath,
138
- rawOutput: counts ? [renderSummaryText(counts), failureRecap].filter(Boolean).join("\n\n") : fallbackOutput
176
+ ...runner.parseResult(captured)
139
177
  };
140
178
  }
141
179
 
142
- async function runWorkspaceScript(root, wsPath, scriptName, junitDestPath, activeChildren) {
143
- const start = Date.now();
144
- const captured = await captureWorkspaceOutput(root, wsPath, scriptName, junitDestPath, activeChildren);
145
- return buildWorkspaceResult(wsPath, junitDestPath, Date.now() - start, captured);
146
- }
147
-
148
180
  async function collectJunitEntries(results) {
149
181
  const entries = [];
150
182
  const missing = [];
@@ -171,14 +203,6 @@ async function runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir,
171
203
  let stopScheduling = false;
172
204
  let nextIndex = 0;
173
205
 
174
- // --ff only stops scheduling new workspaces; ones already running are left to
175
- // finish and report their real result. A launch error is different: it means
176
- // sumlyzer itself can't go on, so nothing still running should keep spawning.
177
- function killActiveChildren() {
178
- for (const child of activeChildren) {
179
- killWorkspaceChild(child);
180
- }
181
- }
182
206
 
183
207
  async function worker() {
184
208
  while (!stopScheduling && nextIndex < workspacesToRun.length) {
@@ -191,11 +215,11 @@ async function runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir,
191
215
 
192
216
  let result;
193
217
  try {
194
- result = await runWorkspaceScript(root, wsPath, scriptName, junitDestPath, activeChildren);
218
+ result = await runWorkspaceScript({ root, wsPath, scriptName, junitDestPath, activeChildren });
195
219
  }
196
220
  catch (error) {
197
221
  stopScheduling = true;
198
- killActiveChildren();
222
+ killActiveChildren(activeChildren);
199
223
  throw new WorkspaceLaunchError(wsPath, scriptName, error);
200
224
  }
201
225
  results[index] = result;
@@ -207,27 +231,21 @@ async function runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir,
207
231
  }
208
232
  }
209
233
 
210
- // Spawned children run detached (their own process group) so a launch error can kill
211
- // a whole npm subtree via killWorkspaceChild. That isolation means Ctrl-C no longer
212
- // reaches them for free the way it would in the same group, so handle it explicitly:
213
- // kill whatever's still running, then exit with the conventional 128+signal code.
214
- function onInterrupt(signal) {
215
- killActiveChildren();
216
- process.exit(signal === "SIGINT" ? 130 : 143);
217
- }
218
- process.once("SIGINT", onInterrupt);
219
- process.once("SIGTERM", onInterrupt);
220
-
221
234
  const workerCount = Math.max(1, Math.min(concurrency, workspacesToRun.length));
222
- try {
223
- await Promise.all(Array.from({ length: workerCount }, worker));
224
- }
225
- finally {
226
- process.off("SIGINT", onInterrupt);
227
- process.off("SIGTERM", onInterrupt);
235
+ await withInterruptHandling(activeChildren, () => Promise.all(Array.from({ length: workerCount }, worker)));
236
+
237
+ const completed = [];
238
+ const skipped = [];
239
+ for (const [index, result] of results.entries()) {
240
+ if (result === undefined) {
241
+ skipped.push(workspacesToRun[index]);
242
+ }
243
+ else {
244
+ completed.push(result);
245
+ }
228
246
  }
229
247
 
230
- return results.filter((result) => result !== undefined);
248
+ return { completed, skipped };
231
249
  }
232
250
 
233
251
  const DEFAULT_JUNIT_FILENAME = "junit.xml";
@@ -264,7 +282,9 @@ export async function main({ root, scriptName, ff, junitPath, concurrency = 1 })
264
282
  throw new NoWorkspacesError();
265
283
  }
266
284
 
267
- const { eligible: workspacesToRun, ownReporterConflicts, invalidPackageJson } = listEligibleWorkspaces(root, workspaces, scriptName);
285
+ const resolvedWorkspaces = await resolveWorkspacePaths(root, workspaces);
286
+
287
+ const { eligible: workspacesToRun, ownReporterConflicts, invalidPackageJson } = listEligibleWorkspaces(root, resolvedWorkspaces, scriptName);
268
288
 
269
289
  if (invalidPackageJson.length > 0) {
270
290
  throw new InvalidPackageJsonError(invalidPackageJson);
@@ -274,14 +294,21 @@ export async function main({ root, scriptName, ff, junitPath, concurrency = 1 })
274
294
  console.log(dim(`⊘ ${workspaceName(wsPath)}: skipped, own --test-reporter detected in its "${scriptName}" script`));
275
295
  }
276
296
 
297
+ if (workspacesToRun.length === 0) {
298
+ console.log(dim(ownReporterConflicts.length > 0
299
+ ? `All workspaces with a "${scriptName}" script were skipped (own --test-reporter detected).`
300
+ : `No workspace has a "${scriptName}" script.`));
301
+ return;
302
+ }
303
+
277
304
  const junitDir = junitPath ? await mkdtemp(path.join(tmpdir(), "sumlyzer-junit-")) : null;
278
305
 
279
- let results;
306
+ let completed, skipped;
280
307
  try {
281
- results = await runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir, concurrency });
308
+ ({ completed, skipped } = await runWorkspaces({ root, workspacesToRun, scriptName, ff, junitDir, concurrency }));
282
309
 
283
310
  if (junitDir) {
284
- await writeJunitReport(root, junitPath, results);
311
+ await writeJunitReport(root, junitPath, completed);
285
312
  }
286
313
  }
287
314
  finally {
@@ -290,6 +317,6 @@ export async function main({ root, scriptName, ff, junitPath, concurrency = 1 })
290
317
  }
291
318
  }
292
319
 
293
- printSummary(results, workspacesToRun.slice(results.length));
294
- reportOutcome(results);
320
+ printSummary(completed, skipped);
321
+ reportOutcome(completed);
295
322
  }
@@ -0,0 +1,38 @@
1
+ import { fileURLToPath } from "node:url";
2
+
3
+ import { stripNpmNoise } from "../parseOutput.mjs";
4
+ import { parseReporterEvents, renderFailureRecap, renderSummaryText } from "../testEventReporter.mjs";
5
+
6
+ const TEST_EVENT_REPORTER_PATH = fileURLToPath(new URL("../testEventReporter.mjs", import.meta.url));
7
+
8
+ const OWN_TEST_REPORTER_FLAG = /--test-reporter(?!-destination)\b/;
9
+
10
+ export function hasOwnReporter(scriptCommand) {
11
+ return OWN_TEST_REPORTER_FLAG.test(scriptCommand);
12
+ }
13
+
14
+ export function prepareEnv(env, junitDestPath) {
15
+ const existing = env.NODE_OPTIONS ? `${env.NODE_OPTIONS} ` : "";
16
+ const testReporterFlags = `--test-reporter="${TEST_EVENT_REPORTER_PATH}" --test-reporter-destination=stdout`;
17
+ const junitFlags = junitDestPath ? ` --test-reporter=junit --test-reporter-destination="${junitDestPath}"` : "";
18
+
19
+ // Drop NODE_TEST_CONTEXT: if sumlyzer itself is invoked from inside a node:test run, this var would otherwise leak.
20
+ const rest = { ...env };
21
+ delete rest.NODE_TEST_CONTEXT;
22
+ return { ...rest, NODE_OPTIONS: `${existing}${testReporterFlags}${junitFlags}` };
23
+ }
24
+
25
+ // Workspaces that don't run through node:test never emit our JSON events; fall
26
+ // back to their raw (npm-noise-stripped) output so failures are still visible.
27
+ export function parseResult({ exitCode, stdout, stderr }) {
28
+ const { counts, failingTests, failures } = parseReporterEvents(stdout);
29
+ const fallbackOutput = stripNpmNoise(stdout + stderr).trim();
30
+ const failureRecap = failures.length > 0 ? renderFailureRecap(failures) : null;
31
+
32
+ return {
33
+ counts,
34
+ failingTests,
35
+ failureDetails: exitCode === 0 ? null : (failureRecap ?? fallbackOutput),
36
+ rawOutput: counts ? [renderSummaryText(counts), failureRecap].filter(Boolean).join("\n\n") : fallbackOutput
37
+ };
38
+ }