Skip to content

Commit

Permalink
feat: event test:bail
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Aug 17, 2023
1 parent a314a59 commit c400ed1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ async function * tapReporter(source) {
case 'test:coverage':
yield getCoverageReport(indent(data.nesting), data.summary, '# ', '');
break;
case 'test:bail':
yield `Bail out! ${tapEscape(data.reason)}\n`;
break;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class Test extends AsyncResource {
this.endTime = hrtime();
this.passed = false;
this.error = err;
if (bail) {
if (bail && !this.root.bailedOut) {
this.root.bailedOut = true;
this.root.postRun();
}
Expand Down Expand Up @@ -754,6 +754,10 @@ class Test extends AsyncResource {
reporter.diagnostic(nesting, loc, diagnostics[i]);
}

if(this.bailedOut){
reporter.bail();
}

reporter.diagnostic(nesting, loc, `tests ${harness.counters.all}`);
reporter.diagnostic(nesting, loc, `suites ${harness.counters.suites}`);
reporter.diagnostic(nesting, loc, `pass ${harness.counters.passed}`);
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/test_runner/tests_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class TestsStream extends Readable {
});
}

bail(reason = ''){
this[kEmitMessage]('test:bail', {
__proto__: null,
reason,
});
}

getSkip(reason = undefined) {
return { __proto__: null, skip: reason ?? true };
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-runner-bail.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('node:test bail tap', () => {
assert.match(child.stdout.toString(), /not ok 2 - second/);
assert.doesNotMatch(child.stdout.toString(), /ok 3 - third/);
assert.match(child.stdout.toString(), /not ok 1 - nested/);
assert.match(child.stdout.toString(), /Bail out!/);
assert.doesNotMatch(child.stdout.toString(), /# Subtest: top level/);
assert.doesNotMatch(child.stdout.toString(), /ok 1 - ok forth/);
assert.doesNotMatch(child.stdout.toString(), /not ok 2 - fifth/);
Expand Down

0 comments on commit c400ed1

Please sign in to comment.