Skip to content

Commit c400ed1

Browse files
feat: event test:bail
1 parent a314a59 commit c400ed1

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

lib/internal/test_runner/reporter/tap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ async function * tapReporter(source) {
6363
case 'test:coverage':
6464
yield getCoverageReport(indent(data.nesting), data.summary, '# ', '');
6565
break;
66+
case 'test:bail':
67+
yield `Bail out! ${tapEscape(data.reason)}\n`;
68+
break;
6669
}
6770
}
6871
}

lib/internal/test_runner/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class Test extends AsyncResource {
503503
this.endTime = hrtime();
504504
this.passed = false;
505505
this.error = err;
506-
if (bail) {
506+
if (bail && !this.root.bailedOut) {
507507
this.root.bailedOut = true;
508508
this.root.postRun();
509509
}
@@ -754,6 +754,10 @@ class Test extends AsyncResource {
754754
reporter.diagnostic(nesting, loc, diagnostics[i]);
755755
}
756756

757+
if(this.bailedOut){
758+
reporter.bail();
759+
}
760+
757761
reporter.diagnostic(nesting, loc, `tests ${harness.counters.all}`);
758762
reporter.diagnostic(nesting, loc, `suites ${harness.counters.suites}`);
759763
reporter.diagnostic(nesting, loc, `pass ${harness.counters.passed}`);

lib/internal/test_runner/tests_stream.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class TestsStream extends Readable {
6262
});
6363
}
6464

65+
bail(reason = ''){
66+
this[kEmitMessage]('test:bail', {
67+
__proto__: null,
68+
reason,
69+
});
70+
}
71+
6572
getSkip(reason = undefined) {
6673
return { __proto__: null, skip: reason ?? true };
6774
}

test/parallel/test-runner-bail.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('node:test bail tap', () => {
3232
assert.match(child.stdout.toString(), /not ok 2 - second/);
3333
assert.doesNotMatch(child.stdout.toString(), /ok 3 - third/);
3434
assert.match(child.stdout.toString(), /not ok 1 - nested/);
35+
assert.match(child.stdout.toString(), /Bail out!/);
3536
assert.doesNotMatch(child.stdout.toString(), /# Subtest: top level/);
3637
assert.doesNotMatch(child.stdout.toString(), /ok 1 - ok forth/);
3738
assert.doesNotMatch(child.stdout.toString(), /not ok 2 - fifth/);

0 commit comments

Comments
 (0)