Skip to content

Commit bd1e3ba

Browse files
committed
test_runner: flatten TAP output when running using --test
1 parent 825efe5 commit bd1e3ba

File tree

9 files changed

+722
-697
lines changed

9 files changed

+722
-697
lines changed

lib/internal/test_runner/runner.js

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const {
88
ArrayPrototypeSlice,
99
ArrayPrototypeSome,
1010
ArrayPrototypeSort,
11+
Number,
1112
ObjectAssign,
13+
ObjectKeys,
1214
PromisePrototypeThen,
13-
SafePromiseAll,
1415
SafePromiseAllReturnVoid,
1516
SafePromiseAllSettledReturnVoid,
1617
SafeMap,
@@ -35,7 +36,14 @@ const { validateArray, validateBoolean } = require('internal/validators');
3536
const { getInspectPort, isUsingInspector, isInspectorMessage } = require('internal/util/inspector');
3637
const { kEmptyObject } = require('internal/util');
3738
const { createTestTree } = require('internal/test_runner/harness');
38-
const { kSubtestsFailed, Test } = require('internal/test_runner/test');
39+
const {
40+
kAborted,
41+
kCancelledByParent,
42+
kSubtestsFailed,
43+
kTestCodeFailure,
44+
kTestTimeoutFailure,
45+
Test,
46+
} = require('internal/test_runner/test');
3947
const { TapParser } = require('internal/test_runner/tap_parser');
4048
const { YAMLToJs } = require('internal/test_runner/yaml_to_js');
4149
const { TokenKind } = require('internal/test_runner/tap_lexer');
@@ -55,6 +63,9 @@ const kFilterArgs = ['--test', '--experimental-test-coverage', '--watch'];
5563
const kFilterArgValues = ['--test-reporter', '--test-reporter-destination'];
5664
const kDiagnosticsFilterArgs = ['tests', 'pass', 'fail', 'cancelled', 'skipped', 'todo', 'duration_ms'];
5765

66+
const kCanceledTests = new SafeSet()
67+
.add(kCancelledByParent).add(kAborted).add(kTestTimeoutFailure);
68+
5869
// TODO(cjihrig): Replace this with recursive readdir once it lands.
5970
function processPath(path, testFiles, options) {
6071
const stats = statSync(path);
@@ -133,6 +144,11 @@ function getRunArgs({ path, inspectPort }) {
133144

134145
class FileTest extends Test {
135146
#buffer = [];
147+
#counters = { __proto__: null, all: 0, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0 };
148+
failedSubtests = false;
149+
#skipReporting() {
150+
return this.#counters.all > 0 && (!this.error || this.error.failureType === kSubtestsFailed);
151+
}
136152
#checkNestedComment({ comment }) {
137153
const firstSpaceIndex = StringPrototypeIndexOf(comment, ' ');
138154
if (firstSpaceIndex === -1) return false;
@@ -141,8 +157,6 @@ class FileTest extends Test {
141157
ArrayPrototypeIncludes(kDiagnosticsFilterArgs, StringPrototypeSlice(comment, 0, firstSpaceIndex));
142158
}
143159
#handleReportItem({ kind, node, comments, nesting = 0 }) {
144-
nesting += 1;
145-
146160
if (comments) {
147161
ArrayPrototypeForEach(comments, (comment) => this.reporter.diagnostic(nesting, this.name, comment));
148162
}
@@ -153,17 +167,20 @@ class FileTest extends Test {
153167
break;
154168

155169
case TokenKind.TAP_PLAN:
170+
if (nesting === 0 && this.#skipReporting()) {
171+
break;
172+
}
156173
this.reporter.plan(nesting, this.name, node.end - node.start + 1);
157174
break;
158175

159176
case TokenKind.TAP_SUBTEST_POINT:
160177
this.reporter.start(nesting, this.name, node.name);
161178
break;
162179

163-
case TokenKind.TAP_TEST_POINT:
164-
// eslint-disable-next-line no-case-declarations
180+
case TokenKind.TAP_TEST_POINT: {
181+
165182
const { todo, skip, pass } = node.status;
166-
// eslint-disable-next-line no-case-declarations
183+
167184
let directive;
168185

169186
if (skip) {
@@ -174,29 +191,21 @@ class FileTest extends Test {
174191
directive = kEmptyObject;
175192
}
176193

177-
if (pass) {
178-
this.reporter.ok(
179-
nesting,
180-
this.name,
181-
node.id,
182-
node.description,
183-
YAMLToJs(node.diagnostics),
184-
directive,
185-
);
186-
} else {
187-
this.reporter.fail(
188-
nesting,
189-
this.name,
190-
node.id,
191-
node.description,
192-
YAMLToJs(node.diagnostics),
193-
directive,
194-
);
194+
const diagnostics = YAMLToJs(node.diagnostics);
195+
const cancelled = kCanceledTests.has(diagnostics.error?.failureType);
196+
const testNumber = nesting === 0 ? (Number(node.id) + this.testNumber - 1) : node.id;
197+
const method = pass ? 'ok' : 'fail';
198+
this.reporter[method](nesting, this.name, testNumber, node.description, diagnostics, directive);
199+
if (nesting === 0) {
200+
super.countSubtest
201+
.call({ finished: true, skipped: skip, isTodo: todo, passed: pass, cancelled }, this.#counters);
202+
this.failedSubtests ||= !pass;
195203
}
196204
break;
197205

206+
}
198207
case TokenKind.COMMENT:
199-
if (nesting === 1 && this.#checkNestedComment(node)) {
208+
if (nesting === 0 && this.#checkNestedComment(node)) {
200209
// Ignore file top level diagnostics
201210
break;
202211
}
@@ -216,10 +225,24 @@ class FileTest extends Test {
216225
this.reportStarted();
217226
this.#handleReportItem(ast);
218227
}
228+
countSubtest(counters) {
229+
if (this.#counters.all === 0) {
230+
return super.countSubtest(counters);
231+
}
232+
ArrayPrototypeForEach(ObjectKeys(counters), (key) => {
233+
counters[key] += this.#counters[key];
234+
});
235+
}
236+
reportStarted() {}
219237
report() {
220-
this.reportStarted();
238+
const skipReporting = this.#skipReporting();
239+
if (!skipReporting) {
240+
super.reportStarted();
241+
}
221242
ArrayPrototypeForEach(this.#buffer, (ast) => this.#handleReportItem(ast));
222-
super.report();
243+
if (!skipReporting) {
244+
super.report();
245+
}
223246
}
224247
}
225248

@@ -274,16 +297,14 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
274297
subtest.addToReport(ast);
275298
});
276299

277-
const { 0: { 0: code, 1: signal } } = await SafePromiseAll([
278-
once(child, 'exit', { signal: t.signal }),
279-
child.stdout.toArray({ signal: t.signal }),
280-
]);
300+
const { 0: code, 1: signal } = await once(child, 'exit', { signal: t.signal });
281301

282302
runningProcesses.delete(path);
283303
runningSubtests.delete(path);
284304
if (code !== 0 || signal !== null) {
285305
if (!err) {
286-
err = ObjectAssign(new ERR_TEST_FAILURE('test failed', kSubtestsFailed), {
306+
const failureType = subtest.failedSubtests ? kSubtestsFailed : kTestCodeFailure;
307+
err = ObjectAssign(new ERR_TEST_FAILURE('test failed', failureType), {
287308
__proto__: null,
288309
exitCode: code,
289310
signal: signal,

lib/internal/test_runner/test.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const { availableParallelism } = require('os');
5757
const { bigint: hrtime } = process.hrtime;
5858
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
5959
const kCancelledByParent = 'cancelledByParent';
60+
const kAborted = 'testAborted';
6061
const kParentAlreadyFinished = 'parentAlreadyFinished';
6162
const kSubtestsFailed = 'subtestsFailed';
6263
const kTestCodeFailure = 'testCodeFailure';
@@ -390,10 +391,12 @@ class Test extends AsyncResource {
390391
}
391392

392393
#abortHandler = () => {
393-
this.cancel(this.#outerSignal?.reason || new AbortError('The test was aborted'));
394+
const error = this.#outerSignal?.reason || new AbortError('The test was aborted');
395+
error.failureType = kAborted;
396+
this.#cancel(error);
394397
};
395398

396-
cancel(error) {
399+
#cancel(error) {
397400
if (this.endTime !== null) {
398401
return;
399402
}
@@ -470,7 +473,7 @@ class Test extends AsyncResource {
470473
return true;
471474
}
472475
if (this.#outerSignal?.aborted) {
473-
this.cancel(this.#outerSignal.reason || new AbortError('The test was aborted'));
476+
this.#abortHandler();
474477
return true;
475478
}
476479
}
@@ -563,7 +566,7 @@ class Test extends AsyncResource {
563566
try { await afterEach(); } catch { /* test is already failing, let's the error */ }
564567
if (isTestFailureError(err)) {
565568
if (err.failureType === kTestTimeoutFailure) {
566-
this.cancel(err);
569+
this.#cancel(err);
567570
} else {
568571
this.fail(err);
569572
}
@@ -577,9 +580,31 @@ class Test extends AsyncResource {
577580
this.postRun();
578581
}
579582

580-
postRun(pendingSubtestsError) {
581-
const counters = { __proto__: null, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0 };
583+
countSubtest(counters) {
584+
// Check SKIP and TODO tests first, as those should not be counted as
585+
// failures.
586+
if (this.skipped) {
587+
counters.skipped++;
588+
} else if (this.isTodo) {
589+
counters.todo++;
590+
} else if (this.cancelled) {
591+
counters.cancelled++;
592+
} else if (!this.passed) {
593+
counters.failed++;
594+
} else {
595+
counters.passed++;
596+
}
597+
598+
if (!this.passed) {
599+
counters.totalFailed++;
600+
}
601+
counters.all++;
602+
}
582603

604+
postRun(pendingSubtestsError) {
605+
const counters = {
606+
__proto__: null, all: 0, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0,
607+
};
583608
// If the test was failed before it even started, then the end time will
584609
// be earlier than the start time. Correct that here.
585610
if (this.endTime < this.startTime) {
@@ -594,27 +619,10 @@ class Test extends AsyncResource {
594619
const subtest = this.subtests[i];
595620

596621
if (!subtest.finished) {
597-
subtest.cancel(pendingSubtestsError);
622+
subtest.#cancel(pendingSubtestsError);
598623
subtest.postRun(pendingSubtestsError);
599624
}
600-
601-
// Check SKIP and TODO tests first, as those should not be counted as
602-
// failures.
603-
if (subtest.skipped) {
604-
counters.skipped++;
605-
} else if (subtest.isTodo) {
606-
counters.todo++;
607-
} else if (subtest.cancelled) {
608-
counters.cancelled++;
609-
} else if (!subtest.passed) {
610-
counters.failed++;
611-
} else {
612-
counters.passed++;
613-
}
614-
615-
if (!subtest.passed) {
616-
counters.totalFailed++;
617-
}
625+
subtest.countSubtest(counters);
618626
}
619627

620628
if ((this.passed || this.parent === null) && counters.totalFailed > 0) {
@@ -634,13 +642,13 @@ class Test extends AsyncResource {
634642
this.parent.processPendingSubtests();
635643
} else if (!this.reported) {
636644
this.reported = true;
637-
this.reporter.plan(this.nesting, kFilename, this.subtests.length);
645+
this.reporter.plan(this.nesting, kFilename, counters.all);
638646

639647
for (let i = 0; i < this.diagnostics.length; i++) {
640648
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i]);
641649
}
642650

643-
this.reporter.diagnostic(this.nesting, kFilename, `tests ${this.subtests.length}`);
651+
this.reporter.diagnostic(this.nesting, kFilename, `tests ${counters.all}`);
644652
this.reporter.diagnostic(this.nesting, kFilename, `pass ${counters.passed}`);
645653
this.reporter.diagnostic(this.nesting, kFilename, `fail ${counters.failed}`);
646654
this.reporter.diagnostic(this.nesting, kFilename, `cancelled ${counters.cancelled}`);
@@ -825,6 +833,8 @@ module.exports = {
825833
kCancelledByParent,
826834
kSubtestsFailed,
827835
kTestCodeFailure,
836+
kTestTimeoutFailure,
837+
kAborted,
828838
kUnwrapErrors,
829839
Suite,
830840
Test,

test/message/test_runner_abort.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ TAP version 13
4040
not ok 7 - not ok 3
4141
---
4242
duration_ms: *
43+
failureType: 'testAborted'
4344
error: 'This operation was aborted'
4445
code: 20
4546
stack: |-
@@ -58,6 +59,7 @@ TAP version 13
5859
not ok 8 - not ok 4
5960
---
6061
duration_ms: *
62+
failureType: 'testAborted'
6163
error: 'This operation was aborted'
6264
code: 20
6365
stack: |-
@@ -76,6 +78,7 @@ TAP version 13
7678
not ok 9 - not ok 5
7779
---
7880
duration_ms: *
81+
failureType: 'testAborted'
7982
error: 'This operation was aborted'
8083
code: 20
8184
stack: |-
@@ -94,6 +97,7 @@ TAP version 13
9497
not ok 1 - promise timeout signal
9598
---
9699
duration_ms: *
100+
failureType: 'testAborted'
97101
error: 'The operation was aborted due to timeout'
98102
code: 23
99103
stack: |-
@@ -106,6 +110,7 @@ not ok 1 - promise timeout signal
106110
not ok 2 - promise abort signal
107111
---
108112
duration_ms: *
113+
failureType: 'testAborted'
109114
error: 'This operation was aborted'
110115
code: 20
111116
stack: |-
@@ -160,6 +165,7 @@ not ok 2 - promise abort signal
160165
not ok 7 - not ok 3
161166
---
162167
duration_ms: *
168+
failureType: 'testAborted'
163169
error: 'This operation was aborted'
164170
code: 20
165171
stack: |-
@@ -178,6 +184,7 @@ not ok 2 - promise abort signal
178184
not ok 8 - not ok 4
179185
---
180186
duration_ms: *
187+
failureType: 'testAborted'
181188
error: 'This operation was aborted'
182189
code: 20
183190
stack: |-
@@ -196,6 +203,7 @@ not ok 2 - promise abort signal
196203
not ok 9 - not ok 5
197204
---
198205
duration_ms: *
206+
failureType: 'testAborted'
199207
error: 'This operation was aborted'
200208
code: 20
201209
stack: |-
@@ -214,6 +222,7 @@ not ok 2 - promise abort signal
214222
not ok 3 - callback timeout signal
215223
---
216224
duration_ms: *
225+
failureType: 'testAborted'
217226
error: 'The operation was aborted due to timeout'
218227
code: 23
219228
stack: |-
@@ -226,6 +235,7 @@ not ok 3 - callback timeout signal
226235
not ok 4 - callback abort signal
227236
---
228237
duration_ms: *
238+
failureType: 'testAborted'
229239
error: 'This operation was aborted'
230240
code: 20
231241
stack: |-

test/message/test_runner_abort_suite.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ TAP version 13
6464
not ok 1 - describe timeout signal
6565
---
6666
duration_ms: *
67+
failureType: 'testAborted'
6768
error: 'The operation was aborted due to timeout'
6869
code: 23
6970
stack: |-
@@ -76,6 +77,7 @@ not ok 1 - describe timeout signal
7677
not ok 2 - describe abort signal
7778
---
7879
duration_ms: *
80+
failureType: 'testAborted'
7981
error: 'This operation was aborted'
8082
code: 20
8183
stack: |-

0 commit comments

Comments
 (0)