Skip to content

Commit d57723d

Browse files
committed
CR
1 parent 0c377ea commit d57723d

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

lib/internal/test_runner/runner.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ArrayPrototypeSort,
1111
Number,
1212
ObjectAssign,
13+
ObjectKeys,
1314
PromisePrototypeThen,
1415
SafePromiseAllReturnVoid,
1516
SafePromiseAllSettledReturnVoid,
@@ -133,9 +134,10 @@ function getRunArgs({ path, inspectPort }) {
133134

134135
class FileTest extends Test {
135136
#buffer = [];
136-
#hasSubtests = false;
137-
get skipReporting() {
138-
return this.#hasSubtests && (!this.error || this.error.failureType === kSubtestsFailed);
137+
#counters = { __proto__: null, all: 0, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0 };
138+
failedSubtests = false;
139+
#skipReporting() {
140+
return this.#counters.all > 0 && (!this.error || this.error.failureType === kSubtestsFailed);
139141
}
140142
#checkNestedComment({ comment }) {
141143
const firstSpaceIndex = StringPrototypeIndexOf(comment, ' ');
@@ -155,7 +157,7 @@ class FileTest extends Test {
155157
break;
156158

157159
case TokenKind.TAP_PLAN:
158-
if (nesting === 0 && this.skipReporting) {
160+
if (nesting === 0 && this.#skipReporting()) {
159161
break;
160162
}
161163
this.reporter.plan(nesting, this.name, node.end - node.start + 1);
@@ -179,15 +181,15 @@ class FileTest extends Test {
179181
directive = kEmptyObject;
180182
}
181183

182-
this.#hasSubtests = true;
183184
const diagnostics = YAMLToJs(node.diagnostics);
184185
const cancelled = diagnostics.error?.failureType === kCancelledByParent;
185186
const testNumber = nesting === 0 ? (Number(node.id) + this.testNumber - 1) : node.id;
186187
const method = pass ? 'ok' : 'fail';
187188
this.reporter[method](nesting, this.name, testNumber, node.description, diagnostics, directive);
188189
if (nesting === 0) {
189-
this.parent.countSubtest({ finished: true, skipped: skip, isTodo: todo, passed: pass, cancelled });
190-
this.countSubtest({ finished: true, skipped: skip, isTodo: todo, passed: pass, cancelled });
190+
super.countSubtest
191+
.call({ finished: true, skipped: skip, isTodo: todo, passed: pass, cancelled }, this.#counters);
192+
this.failedSubtests ||= !pass;
191193
}
192194
break;
193195

@@ -213,13 +215,22 @@ class FileTest extends Test {
213215
this.reportStarted();
214216
this.#handleReportItem(ast);
215217
}
218+
countSubtest(counters) {
219+
if (this.#counters.all === 0) {
220+
return super.countSubtest(counters);
221+
}
222+
ArrayPrototypeForEach(ObjectKeys(counters), (key) => {
223+
counters[key] += this.#counters[key];
224+
});
225+
}
216226
reportStarted() {}
217227
report() {
218-
if (!this.skipReporting) {
228+
const skipReporting = this.#skipReporting();
229+
if (!skipReporting) {
219230
super.reportStarted();
220231
}
221232
ArrayPrototypeForEach(this.#buffer, (ast) => this.#handleReportItem(ast));
222-
if (!this.skipReporting) {
233+
if (!skipReporting) {
223234
super.report();
224235
}
225236
}
@@ -282,7 +293,7 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
282293
runningSubtests.delete(path);
283294
if (code !== 0 || signal !== null) {
284295
if (!err) {
285-
const failureType = subtest.completedCounters.totalFailed > 0 ? kSubtestsFailed : kTestCodeFailure;
296+
const failureType = subtest.failedSubtests ? kSubtestsFailed : kTestCodeFailure;
286297
err = ObjectAssign(new ERR_TEST_FAILURE('test failed', failureType), {
287298
__proto__: null,
288299
exitCode: code,

lib/internal/test_runner/test.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ class Test extends AsyncResource {
286286
beforeEach: [],
287287
afterEach: [],
288288
};
289-
this.completedCounters = {
290-
__proto__: null,
291-
all: 0, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0
292-
};
293289
this.waitingOn = 0;
294290
this.finished = false;
295291
}
@@ -582,32 +578,31 @@ class Test extends AsyncResource {
582578
this.postRun();
583579
}
584580

585-
countSubtest(subtest) {
586-
if (subtest.skipReporting || subtest.counted) {
587-
return;
588-
}
589-
subtest.counted = true;
581+
countSubtest(counters) {
590582
// Check SKIP and TODO tests first, as those should not be counted as
591583
// failures.
592-
if (subtest.skipped) {
593-
this.completedCounters.skipped++;
594-
} else if (subtest.isTodo) {
595-
this.completedCounters.todo++;
596-
} else if (subtest.cancelled) {
597-
this.completedCounters.cancelled++;
598-
} else if (!subtest.passed) {
599-
this.completedCounters.failed++;
584+
if (this.skipped) {
585+
counters.skipped++;
586+
} else if (this.isTodo) {
587+
counters.todo++;
588+
} else if (this.cancelled) {
589+
counters.cancelled++;
590+
} else if (!this.passed) {
591+
counters.failed++;
600592
} else {
601-
this.completedCounters.passed++;
593+
counters.passed++;
602594
}
603595

604-
if (!subtest.passed) {
605-
this.completedCounters.totalFailed++;
596+
if (!this.passed) {
597+
counters.totalFailed++;
606598
}
607-
this.completedCounters.all++;
599+
counters.all++;
608600
}
609601

610602
postRun(pendingSubtestsError) {
603+
const counters = {
604+
__proto__: null, all: 0, failed: 0, passed: 0, cancelled: 0, skipped: 0, todo: 0, totalFailed: 0
605+
};
611606
// If the test was failed before it even started, then the end time will
612607
// be earlier than the start time. Correct that here.
613608
if (this.endTime < this.startTime) {
@@ -620,15 +615,17 @@ class Test extends AsyncResource {
620615
this.pendingSubtests = [];
621616
for (let i = 0; i < this.subtests.length; i++) {
622617
const subtest = this.subtests[i];
618+
623619
if (!subtest.finished) {
624620
subtest.cancel(pendingSubtestsError);
625621
subtest.postRun(pendingSubtestsError);
626622
}
623+
subtest.countSubtest(counters);
627624
}
628625

629-
if ((this.passed || this.parent === null) && this.completedCounters.totalFailed > 0) {
630-
const subtestString = `subtest${this.completedCounters.totalFailed > 1 ? 's' : ''}`;
631-
const msg = `${this.completedCounters.totalFailed} ${subtestString} failed`;
626+
if ((this.passed || this.parent === null) && counters.totalFailed > 0) {
627+
const subtestString = `subtest${counters.totalFailed > 1 ? 's' : ''}`;
628+
const msg = `${counters.totalFailed} ${subtestString} failed`;
632629

633630
this.fail(new ERR_TEST_FAILURE(msg, kSubtestsFailed));
634631
}
@@ -641,21 +638,20 @@ class Test extends AsyncResource {
641638
this.parent.addReadySubtest(this);
642639
this.parent.processReadySubtestRange(false);
643640
this.parent.processPendingSubtests();
644-
this.parent.countSubtest(this);
645641
} else if (!this.reported) {
646642
this.reported = true;
647-
this.reporter.plan(this.nesting, kFilename, this.completedCounters.all);
643+
this.reporter.plan(this.nesting, kFilename, counters.all);
648644

649645
for (let i = 0; i < this.diagnostics.length; i++) {
650646
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i]);
651647
}
652648

653-
this.reporter.diagnostic(this.nesting, kFilename, `tests ${this.completedCounters.all}`);
654-
this.reporter.diagnostic(this.nesting, kFilename, `pass ${this.completedCounters.passed}`);
655-
this.reporter.diagnostic(this.nesting, kFilename, `fail ${this.completedCounters.failed}`);
656-
this.reporter.diagnostic(this.nesting, kFilename, `cancelled ${this.completedCounters.cancelled}`);
657-
this.reporter.diagnostic(this.nesting, kFilename, `skipped ${this.completedCounters.skipped}`);
658-
this.reporter.diagnostic(this.nesting, kFilename, `todo ${this.completedCounters.todo}`);
649+
this.reporter.diagnostic(this.nesting, kFilename, `tests ${counters.all}`);
650+
this.reporter.diagnostic(this.nesting, kFilename, `pass ${counters.passed}`);
651+
this.reporter.diagnostic(this.nesting, kFilename, `fail ${counters.failed}`);
652+
this.reporter.diagnostic(this.nesting, kFilename, `cancelled ${counters.cancelled}`);
653+
this.reporter.diagnostic(this.nesting, kFilename, `skipped ${counters.skipped}`);
654+
this.reporter.diagnostic(this.nesting, kFilename, `todo ${counters.todo}`);
659655
this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`);
660656

661657
if (this.coverage) {

test/parallel/test-runner-extraneous-async-activity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const { spawnSync } = require('child_process');
1111
]);
1212
const stdout = child.stdout.toString();
1313
assert.match(stdout, /^# Warning: Test "extraneous async activity test" generated asynchronous activity after the test ended/m);
14-
assert.match(stdout, /^# pass 1$/m);
15-
assert.match(stdout, /^# fail 1$/m);
14+
assert.match(stdout, /^# pass 1/m);
15+
assert.match(stdout, /^# fail 0$/m);
1616
assert.match(stdout, /^# cancelled 0$/m);
1717
assert.strictEqual(child.status, 1);
1818
assert.strictEqual(child.signal, null);
@@ -26,7 +26,7 @@ const { spawnSync } = require('child_process');
2626
const stdout = child.stdout.toString();
2727
assert.match(stdout, /^# Warning: Test "extraneous async activity test" generated asynchronous activity after the test ended/m);
2828
assert.match(stdout, /^# pass 1$/m);
29-
assert.match(stdout, /^# fail 1$/m);
29+
assert.match(stdout, /^# fail 0$/m);
3030
assert.match(stdout, /^# cancelled 0$/m);
3131
assert.strictEqual(child.status, 1);
3232
assert.strictEqual(child.signal, null);

0 commit comments

Comments
 (0)