Skip to content

Commit 55a4b28

Browse files
committed
test_runner: report failing tests after summary
Re-output failing tests after summary has been printed. This behavior follows other popular test runners (e.g. jest, mocha, etc...). Updated SpecReporter: 1. When there is a 'test:fail' event, the test will be stored. 2. After no more input, all the failed tests will be flushed. 3. Extract the logic for formatting a test report into a re-usable function. Fixes: nodejs#47110
1 parent 30d92e8 commit 55a4b28

File tree

3 files changed

+256
-15
lines changed

3 files changed

+256
-15
lines changed

lib/internal/test_runner/reporter/spec.js

+34-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeJoin,
55
ArrayPrototypePop,
6+
ArrayPrototypePush,
67
ArrayPrototypeShift,
78
ArrayPrototypeUnshift,
89
hardenRegExp,
@@ -36,6 +37,7 @@ class SpecReporter extends Transform {
3637
#stack = [];
3738
#reported = [];
3839
#indentMemo = new SafeMap();
40+
#failedTests = [];
3941

4042
constructor() {
4143
super({ writableObjectMode: true });
@@ -60,12 +62,26 @@ class SpecReporter extends Transform {
6062
), `\n${indent} `);
6163
return `\n${indent} ${message}\n`;
6264
}
63-
#handleEvent({ type, data }) {
65+
#formatTestReport(type, data, prefix = '', indent = '', hasChildren = false, skippedSubtest = false) {
6466
let color = colors[type] ?? white;
6567
let symbol = symbols[type] ?? ' ';
66-
68+
const duration_ms = data.details?.duration_ms ? ` ${gray}(${data.details.duration_ms}ms)${white}` : '';
69+
const title = `${data.name}${duration_ms}${skippedSubtest ? ' # SKIP' : ''}`;
70+
if (hasChildren) {
71+
// If this test has had children - it was already reported, so slightly modify the output
72+
return `${prefix}${indent}${color}${symbols['arrow:right']}${white}${title}\n`;
73+
}
74+
const error = this.#formatError(data.details?.error, indent);
75+
if (skippedSubtest) {
76+
color = gray;
77+
symbol = symbols['hyphen:minus'];
78+
}
79+
return `${prefix}${indent}${color}${symbol}${title}${error}${white}`;
80+
}
81+
#handleEvent({ type, data }) {
6782
switch (type) {
6883
case 'test:fail':
84+
ArrayPrototypePush(this.#failedTests, data);
6985
case 'test:pass': {
7086
const subtest = ArrayPrototypeShift(this.#stack); // This is the matching `test:start` event
7187
if (subtest) {
@@ -82,32 +98,35 @@ class SpecReporter extends Transform {
8298
ArrayPrototypeUnshift(this.#reported, msg);
8399
prefix += `${this.#indent(msg.nesting)}${symbols['arrow:right']}${msg.name}\n`;
84100
}
85-
const skippedSubtest = subtest && data.skip && data.skip !== undefined;
86-
const indent = this.#indent(data.nesting);
87-
const duration_ms = data.details?.duration_ms ? ` ${gray}(${data.details.duration_ms}ms)${white}` : '';
88-
const title = `${data.name}${duration_ms}${skippedSubtest ? ' # SKIP' : ''}`;
101+
let hasChildren = false;
89102
if (this.#reported[0] && this.#reported[0].nesting === data.nesting && this.#reported[0].name === data.name) {
90-
// If this test has had children - it was already reported, so slightly modify the output
91103
ArrayPrototypeShift(this.#reported);
92-
return `${prefix}${indent}${color}${symbols['arrow:right']}${white}${title}\n\n`;
93-
}
94-
const error = this.#formatError(data.details?.error, indent);
95-
if (skippedSubtest) {
96-
color = gray;
97-
symbol = symbols['hyphen:minus'];
104+
hasChildren = true;
98105
}
99-
return `${prefix}${indent}${color}${symbol}${title}${error}${white}\n`;
106+
const skippedSubtest = subtest && data.skip && data.skip !== undefined;
107+
const indent = this.#indent(data.nesting);
108+
return `${this.#formatTestReport(type, data, prefix, indent, hasChildren, skippedSubtest)}\n`;
100109
}
101110
case 'test:start':
102111
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
103112
break;
104113
case 'test:diagnostic':
105-
return `${color}${this.#indent(data.nesting)}${symbol}${data.message}${white}\n`;
114+
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
106115
}
107116
}
108117
_transform({ type, data }, encoding, callback) {
109118
callback(null, this.#handleEvent({ type, data }));
110119
}
120+
_flush(callback) {
121+
const results = [`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
122+
for (let i = 0; i < this.#failedTests.length; i++) {
123+
ArrayPrototypePush(results, this.#formatTestReport(
124+
'test:fail',
125+
this.#failedTests[i],
126+
));
127+
}
128+
callback(null, ArrayPrototypeJoin(results, '\n'));
129+
}
111130
}
112131

113132
module.exports = SpecReporter;

test/message/test_runner_output_spec_reporter.out

+209
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,212 @@
282282
skipped 10
283283
todo 5
284284
duration_ms *
285+
286+
failing tests:
287+
288+
sync fail todo (*ms)
289+
Error: thrown from sync fail todo
290+
*
291+
*
292+
*
293+
*
294+
*
295+
*
296+
*
297+
298+
sync fail todo with message (*ms)
299+
Error: thrown from sync fail todo with message
300+
*
301+
*
302+
*
303+
*
304+
*
305+
*
306+
*
307+
308+
sync throw fail (*ms)
309+
Error: thrown from sync throw fail
310+
*
311+
*
312+
*
313+
*
314+
*
315+
*
316+
*
317+
318+
async throw fail (*ms)
319+
Error: thrown from async throw fail
320+
*
321+
*
322+
*
323+
*
324+
*
325+
*
326+
*
327+
328+
async skip fail (*ms)
329+
Error: thrown from async throw fail
330+
*
331+
*
332+
*
333+
*
334+
*
335+
*
336+
*
337+
338+
async assertion fail (*ms)
339+
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
340+
341+
true !== false
342+
343+
*
344+
*
345+
*
346+
*
347+
*
348+
*
349+
* {
350+
generatedMessage: true,
351+
code: 'ERR_ASSERTION',
352+
actual: true,
353+
expected: false,
354+
operator: 'strictEqual'
355+
}
356+
357+
reject fail (*ms)
358+
Error: rejected from reject fail
359+
*
360+
*
361+
*
362+
*
363+
*
364+
*
365+
*
366+
367+
+sync throw fail (*ms)
368+
Error: thrown from subtest sync throw fail
369+
*
370+
*
371+
*
372+
*
373+
*
374+
*
375+
*
376+
*
377+
*
378+
*
379+
380+
subtest sync throw fail (*ms)
381+
'1 subtest failed'
382+
383+
sync throw non-error fail (*ms)
384+
Symbol(thrown symbol from sync throw non-error fail)
385+
386+
+long running (*ms)
387+
'test did not finish before its parent and was cancelled'
388+
389+
top level (*ms)
390+
'1 subtest failed'
391+
392+
sync skip option is false fail (*ms)
393+
Error: this should be executed
394+
*
395+
*
396+
*
397+
*
398+
*
399+
*
400+
*
401+
402+
callback fail (*ms)
403+
Error: callback failure
404+
*
405+
*
406+
407+
callback also returns a Promise (*ms)
408+
'passed a callback but also returned a Promise'
409+
410+
callback throw (*ms)
411+
Error: thrown from callback throw
412+
*
413+
*
414+
*
415+
*
416+
*
417+
*
418+
*
419+
420+
callback called twice (*ms)
421+
'callback invoked multiple times'
422+
423+
callback called twice in future tick (*ms)
424+
Error [ERR_TEST_FAILURE]: callback invoked multiple times
425+
*
426+
failureType: 'multipleCallbackInvocations',
427+
cause: 'callback invoked multiple times',
428+
code: 'ERR_TEST_FAILURE'
429+
}
430+
431+
callback async throw (*ms)
432+
Error: thrown from callback async throw
433+
*
434+
*
435+
436+
custom inspect symbol fail (*ms)
437+
customized
438+
439+
custom inspect symbol that throws fail (*ms)
440+
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
441+
442+
sync throw fails at first (*ms)
443+
Error: thrown from subtest sync throw fails at first
444+
*
445+
*
446+
*
447+
*
448+
*
449+
*
450+
*
451+
*
452+
*
453+
*
454+
455+
sync throw fails at second (*ms)
456+
Error: thrown from subtest sync throw fails at second
457+
*
458+
*
459+
*
460+
*
461+
*
462+
*
463+
*
464+
*
465+
*
466+
*
467+
468+
subtest sync throw fails (*ms)
469+
'2 subtests failed'
470+
471+
timed out async test (*ms)
472+
'test timed out after 5ms'
473+
474+
timed out callback test (*ms)
475+
'test timed out after 5ms'
476+
477+
rejected thenable (*ms)
478+
'custom error'
479+
480+
unfinished test with uncaughtException (*ms)
481+
Error: foo
482+
*
483+
*
484+
*
485+
486+
unfinished test with unhandledRejection (*ms)
487+
Error: bar
488+
*
489+
*
490+
*
491+
492+
invalid subtest fail (*ms)
493+
'test could not be started because its parent finished'

test/pseudo-tty/test_runner_default_reporter.out

+13
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@
1717
[34m* skipped 1[39m
1818
[34m* todo 0[39m
1919
[34m* duration_ms *[39m
20+
21+
[31m* failing tests:[39m
22+
23+
[31m* should fail [90m(*ms)[39m
24+
Error: fail
25+
at * [90m(*)[39m
26+
[90m at *[39m
27+
[90m at *[39m
28+
[90m at *[39m
29+
[90m at *[39m
30+
[90m at *[39m
31+
[90m at *[39m
32+
[39m

0 commit comments

Comments
 (0)