From be12fbdbe55b034e4ad7ee503ec007b21d77bbbb Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 18 Jul 2023 14:50:57 +0200 Subject: [PATCH 01/25] test_runner: test runner bail --- doc/api/cli.md | 11 ++++++++ doc/api/errors.md | 7 +++++ doc/api/test.md | 18 +++++++++++++ lib/internal/errors.js | 4 +++ lib/internal/main/test_runner.js | 7 ++++- lib/internal/test_runner/test.js | 18 +++++++++---- lib/internal/test_runner/utils.js | 2 ++ src/node_options.cc | 3 +++ src/node_options.h | 1 + test/fixtures/test-runner/bail/bail.js | 18 +++++++++++++ test/parallel/test-runner-bail.js | 37 ++++++++++++++++++++++++++ 11 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/test-runner/bail/bail.js create mode 100644 test/parallel/test-runner-bail.js diff --git a/doc/api/cli.md b/doc/api/cli.md index f590ebd0c89deb..0c25cb88a3e4fd 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1507,6 +1507,16 @@ Starts the Node.js command line test runner. This flag cannot be combined with See the documentation on [running tests from the command line][] for more details. +### `--test-bail` + + + +Specifies the bailout behavior of the test runner when running tests. +See the documentation on [test bailout][] for more details. + ### `--test-name-pattern` + +```bash +node --test-bail +``` + +The `--test-bail` flag provides a way to stop the test execution +as soon as a test fails. +By enabling this flag, the test runner will exit the test suite early +when it encounters the first failing test, preventing +the execution of subsequent tests. +**Default:** `false`. + ## Test reporters -Specifies the bailout behavior of the test runner when running tests. +Instructs the test runner to bail out if a test failure occurs. See the documentation on [test bailout][] for more details. ### `--test-name-pattern` diff --git a/doc/api/errors.md b/doc/api/errors.md index d22bd5fc91d5ca..5a8637a0ecc8b0 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2721,13 +2721,6 @@ the token causing the error is available via the `cause` property. This error represents a failed TAP validation. - - -### `ERR_TEST_BAILOUT` - -This error represents a test that has bailed out after failure. -This error occurs only when the flag `--test-bail` is passed. - ### `ERR_TEST_FAILURE` diff --git a/doc/api/test.md b/doc/api/test.md index 3bfaea47d8988f..71fc7b649abdbc 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -602,7 +602,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w }); ``` -## Test bail +## Bailing out -```bash -node --test-bail -``` - The `--test-bail` flag provides a way to stop the test execution as soon as a test fails. By enabling this flag, the test runner will exit the test suite early diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index ba2b1afecf7437..915362b411b4e5 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -14,6 +14,7 @@ const { const { inspectWithNoCustomRetry } = require('internal/errors'); const { isError, kEmptyObject } = require('internal/util'); const { getCoverageReport } = require('internal/test_runner/utils'); +const { kBailedOut } = require('internal/test_runner/test'); const kDefaultIndent = ' '; // 4 spaces const kFrameStartRegExp = /^ {4}at /; const kLineBreakRegExp = /\n|\r\n/; diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 622f8488a44f81..43b7de41a54407 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -981,6 +981,7 @@ module.exports = { kSubtestsFailed, kTestCodeFailure, kTestTimeoutFailure, + kBailedOut, kAborted, kUnwrapErrors, Suite, diff --git a/src/node_options.cc b/src/node_options.cc index a9905604bb10e9..80933b4ee60ee7 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -567,9 +567,6 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "profile generated with --heap-prof. (default: 512 * 1024)", &EnvironmentOptions::heap_prof_interval); #endif // HAVE_INSPECTOR - AddOption("--test-bail", - "stop test execution when a test has failed", - &EnvironmentOptions::test_bail); AddOption("--max-http-header-size", "set the maximum size of HTTP headers (default: 16384 (16KB))", &EnvironmentOptions::max_http_header_size, @@ -609,6 +606,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "run test at specific shard", &EnvironmentOptions::test_shard, kAllowedInEnvvar); + AddOption("--test-bail", + "stop test runner execution on the first test failure", + &EnvironmentOptions::test_bail); AddOption("--test-udp-no-try-send", "", // For testing only. &EnvironmentOptions::test_udp_no_try_send); AddOption("--throw-deprecation", diff --git a/test/fixtures/test-runner/bail/bail.js b/test/fixtures/test-runner/bail/bail.js index 20321b87001d67..7c70b0ebb339e1 100644 --- a/test/fixtures/test-runner/bail/bail.js +++ b/test/fixtures/test-runner/bail/bail.js @@ -1,3 +1,4 @@ +'use strict'; const test = require('node:test'); test('nested', (t) => { diff --git a/test/fixtures/test-runner/bail/error.js b/test/fixtures/test-runner/bail/error.js index 2d0c8bdaea3cee..1dddbee1e38ab8 100644 --- a/test/fixtures/test-runner/bail/error.js +++ b/test/fixtures/test-runner/bail/error.js @@ -1,12 +1,12 @@ -const assert = require('assert'); +'use strict'; const common = require('../../../common'); - +const assert = require('assert'); const test = require('node:test'); test('keep error', (t) => { - assert.strictEqual(0, 1); + assert.strictEqual(0, 1); }); -test('dont show', common.mustNotCall( t => { - assert.strictEqual(0, 2); +test('dont show', common.mustNotCall((t) => { + assert.strictEqual(0, 2); })); diff --git a/test/parallel/test-runner-bail.js b/test/parallel/test-runner-bail.js index 99edca43be9b21..1fd6ebf48e9a39 100644 --- a/test/parallel/test-runner-bail.js +++ b/test/parallel/test-runner-bail.js @@ -13,7 +13,7 @@ const errorTestFile = fixtures.path('test-runner/bail/error.js'); tmpdir.refresh(); describe('maintain errors', () => { - it('should exit at assertion failure', () => { + it('should exit on first failure', () => { const child = spawnSync(process.execPath, ['--test', '--test-bail', errorTestFile]); assert.strictEqual(child.stderr.toString(), ''); assert.match(child.stdout.toString(), /failureType: 'testCodeFailure'/); @@ -35,20 +35,6 @@ describe('node:test bail tap', () => { 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/); - - }); - - it('should exit not exit if bail isnt set', () => { - const child = spawnSync(process.execPath, ['--test', testFile]); - assert.strictEqual(child.stderr.toString(), ''); - assert.match(child.stdout.toString(), /ok 1 - first/); - assert.match(child.stdout.toString(), /not ok 2 - second/); - assert.match(child.stdout.toString(), /not ok 3 - third/); - assert.match(child.stdout.toString(), /not ok 1 - nested/); - assert.match(child.stdout.toString(), /# Subtest: top level/); - assert.match(child.stdout.toString(), /ok 1 - forth/); - assert.match(child.stdout.toString(), /not ok 2 - fifth/); - assert.match(child.stdout.toString(), /not ok 2 - top level/); }); }); @@ -62,14 +48,4 @@ describe('node:test bail spec', () => { assert.doesNotMatch(child.stdout.toString(), /✔ forth/); assert.doesNotMatch(child.stdout.toString(), /✖ fifth/); }); - - it('should exit not exit if bail isnt set', () => { - const child = spawnSync(process.execPath, ['--test', '--test-reporter=spec', testFile]); - assert.strictEqual(child.stderr.toString(), ''); - assert.match(child.stdout.toString(), /✔ first/); - assert.match(child.stdout.toString(), /✖ second/); - assert.match(child.stdout.toString(), /✖ third/); - assert.match(child.stdout.toString(), /✔ forth/); - assert.match(child.stdout.toString(), /✖ fifth/); - }); }); From e91c6802404b183097eb79ab7a3026dcb7061caa Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 14 Aug 2023 12:13:53 +0200 Subject: [PATCH 20/25] chore: mark as experimental --- doc/api/cli.md | 2 ++ doc/api/test.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/doc/api/cli.md b/doc/api/cli.md index afeb5059238f83..88b4d5a9827f0a 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1514,6 +1514,8 @@ added: - REPLACEME --> +> Stability: 1 - Experimental + Instructs the test runner to bail out if a test failure occurs. See the documentation on [test bailout][] for more details. diff --git a/doc/api/test.md b/doc/api/test.md index 292a5e78464bda..6aa74fce1a5cdf 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -609,6 +609,8 @@ added: - REPLACEME --> +> Stability: 1 - Experimental + The `--test-bail` flag provides a way to stop the test execution as soon as a test fails. By enabling this flag, the test runner will exit the test suite early From 6e012a1e2e690b21fdf781df6a72af8973bdc31e Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 14 Aug 2023 12:14:16 +0200 Subject: [PATCH 21/25] feat: event test:bail --- lib/internal/test_runner/reporter/tap.js | 3 +++ lib/internal/test_runner/test.js | 6 +++++- lib/internal/test_runner/tests_stream.js | 7 +++++++ test/parallel/test-runner-bail.js | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index 915362b411b4e5..9e6a819181d3c1 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -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; } } } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 43b7de41a54407..77f5b7677aa887 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -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(); } @@ -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}`); diff --git a/lib/internal/test_runner/tests_stream.js b/lib/internal/test_runner/tests_stream.js index f7730caac00fa7..69630963886123 100644 --- a/lib/internal/test_runner/tests_stream.js +++ b/lib/internal/test_runner/tests_stream.js @@ -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 }; } diff --git a/test/parallel/test-runner-bail.js b/test/parallel/test-runner-bail.js index 1fd6ebf48e9a39..109cb5dbcbac64 100644 --- a/test/parallel/test-runner-bail.js +++ b/test/parallel/test-runner-bail.js @@ -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/); From f9d176523eacd2b46b42d862cf809e1e06499265 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 14 Aug 2023 12:31:23 +0200 Subject: [PATCH 22/25] fix: more bailout --- lib/internal/test_runner/reporter/tap.js | 2 +- lib/internal/test_runner/test.js | 13 ++++++------- lib/internal/test_runner/tests_stream.js | 3 ++- test/fixtures/test-runner/bail/bail.js | 3 ++- test/fixtures/test-runner/output/bail.js | 19 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 1 + 6 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/test-runner/output/bail.js diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index 9e6a819181d3c1..ba396c640feb4e 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -64,7 +64,7 @@ async function * tapReporter(source) { yield getCoverageReport(indent(data.nesting), data.summary, '# ', ''); break; case 'test:bail': - yield `Bail out! ${tapEscape(data.reason)}\n`; + yield `${indent(data.nesting)}Bail out! ${tapEscape(data.reason)}\n`; break; } } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 77f5b7677aa887..041d94f38960ff 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -504,6 +504,7 @@ class Test extends AsyncResource { this.passed = false; this.error = err; if (bail && !this.root.bailedOut) { + this.bailedOut = true; this.root.bailedOut = true; this.root.postRun(); } @@ -582,12 +583,6 @@ class Test extends AsyncResource { } async run() { - if (bailedOut) { - if (this.parent !== null) { - this.parent.postRun(); - } - return; - } if (this.parent !== null) { this.parent.activeSubtests++; } @@ -727,6 +722,10 @@ class Test extends AsyncResource { this.parent.processReadySubtestRange(false); this.parent.processPendingSubtests(); + if (this.bailedOut) { + this.reporter.bail(this.nesting); + } + if (this.parent === this.root && this.root.activeSubtests === 0 && this.root.pendingSubtests.length === 0 && @@ -754,7 +753,7 @@ class Test extends AsyncResource { reporter.diagnostic(nesting, loc, diagnostics[i]); } - if(this.bailedOut){ + if (this.bailedOut) { reporter.bail(); } diff --git a/lib/internal/test_runner/tests_stream.js b/lib/internal/test_runner/tests_stream.js index 69630963886123..b9d0afb0f26b2b 100644 --- a/lib/internal/test_runner/tests_stream.js +++ b/lib/internal/test_runner/tests_stream.js @@ -62,9 +62,10 @@ class TestsStream extends Readable { }); } - bail(reason = ''){ + bail(nesting, reason = '') { this[kEmitMessage]('test:bail', { __proto__: null, + nesting, reason, }); } diff --git a/test/fixtures/test-runner/bail/bail.js b/test/fixtures/test-runner/bail/bail.js index 7c70b0ebb339e1..9e4ad8f887a0d8 100644 --- a/test/fixtures/test-runner/bail/bail.js +++ b/test/fixtures/test-runner/bail/bail.js @@ -1,4 +1,5 @@ 'use strict'; +const common = require('../../../common'); const test = require('node:test'); test('nested', (t) => { @@ -6,7 +7,7 @@ test('nested', (t) => { t.test('second', () => { throw new Error(); }); - t.test('third', () => {}); + t.test('third', common.mustNotCall(() => {})); }); test('top level', (t) => { diff --git a/test/fixtures/test-runner/output/bail.js b/test/fixtures/test-runner/output/bail.js new file mode 100644 index 00000000000000..5c168bc03d04eb --- /dev/null +++ b/test/fixtures/test-runner/output/bail.js @@ -0,0 +1,19 @@ +// Flags: --test --test-bail +'use strict'; +const common = require('../../../common'); +const test = require('node:test'); + +test('nested', (t) => { + t.test('first', () => {}); + t.test('second', () => { + throw new Error(); + }); + t.test('third', common.mustNotCall(() => {})); +}); + +test('top level', (t) => { + t.test('forth', () => {}); + t.test('fifth', () => { + throw new Error(); + }); +}); diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 8db41bff38a114..ba84d9ea8e0430 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -75,6 +75,7 @@ const tests = [ { name: 'test-runner/output/default_output.js', transform: specTransform, tty: true }, { name: 'test-runner/output/arbitrary-output.js' }, { name: 'test-runner/output/async-test-scheduling.mjs' }, + { name: 'test-runner/output/abort.js' }, !skipForceColors ? { name: 'test-runner/output/arbitrary-output-colored.js', transform: snapshot.transform(specTransform, replaceTestDuration), tty: true From c508639e3681563ec08bbb1998aff9d2481c0570 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 29 Aug 2023 12:10:27 +0200 Subject: [PATCH 23/25] Revert "fix: more bailout" This reverts commit c4e36541d846fca851137b9413d63c444d7ae0ba. --- lib/internal/test_runner/reporter/tap.js | 2 +- lib/internal/test_runner/test.js | 13 +++++++------ lib/internal/test_runner/tests_stream.js | 3 +-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index ba396c640feb4e..9e6a819181d3c1 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -64,7 +64,7 @@ async function * tapReporter(source) { yield getCoverageReport(indent(data.nesting), data.summary, '# ', ''); break; case 'test:bail': - yield `${indent(data.nesting)}Bail out! ${tapEscape(data.reason)}\n`; + yield `Bail out! ${tapEscape(data.reason)}\n`; break; } } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 041d94f38960ff..77f5b7677aa887 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -504,7 +504,6 @@ class Test extends AsyncResource { this.passed = false; this.error = err; if (bail && !this.root.bailedOut) { - this.bailedOut = true; this.root.bailedOut = true; this.root.postRun(); } @@ -583,6 +582,12 @@ class Test extends AsyncResource { } async run() { + if (bailedOut) { + if (this.parent !== null) { + this.parent.postRun(); + } + return; + } if (this.parent !== null) { this.parent.activeSubtests++; } @@ -722,10 +727,6 @@ class Test extends AsyncResource { this.parent.processReadySubtestRange(false); this.parent.processPendingSubtests(); - if (this.bailedOut) { - this.reporter.bail(this.nesting); - } - if (this.parent === this.root && this.root.activeSubtests === 0 && this.root.pendingSubtests.length === 0 && @@ -753,7 +754,7 @@ class Test extends AsyncResource { reporter.diagnostic(nesting, loc, diagnostics[i]); } - if (this.bailedOut) { + if(this.bailedOut){ reporter.bail(); } diff --git a/lib/internal/test_runner/tests_stream.js b/lib/internal/test_runner/tests_stream.js index b9d0afb0f26b2b..69630963886123 100644 --- a/lib/internal/test_runner/tests_stream.js +++ b/lib/internal/test_runner/tests_stream.js @@ -62,10 +62,9 @@ class TestsStream extends Readable { }); } - bail(nesting, reason = '') { + bail(reason = ''){ this[kEmitMessage]('test:bail', { __proto__: null, - nesting, reason, }); } From 5b698046b28ba0b0f9ae4779dc7bbbaa1ac320f3 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 29 Aug 2023 12:10:42 +0200 Subject: [PATCH 24/25] Revert "feat: event test:bail" This reverts commit c400ed154de1f919aeedbd94127494de5e9ba491. --- lib/internal/test_runner/reporter/tap.js | 3 --- lib/internal/test_runner/test.js | 6 +----- lib/internal/test_runner/tests_stream.js | 7 ------- test/parallel/test-runner-bail.js | 1 - 4 files changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index 9e6a819181d3c1..915362b411b4e5 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -63,9 +63,6 @@ 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; } } } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 77f5b7677aa887..43b7de41a54407 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -503,7 +503,7 @@ class Test extends AsyncResource { this.endTime = hrtime(); this.passed = false; this.error = err; - if (bail && !this.root.bailedOut) { + if (bail) { this.root.bailedOut = true; this.root.postRun(); } @@ -754,10 +754,6 @@ 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}`); diff --git a/lib/internal/test_runner/tests_stream.js b/lib/internal/test_runner/tests_stream.js index 69630963886123..f7730caac00fa7 100644 --- a/lib/internal/test_runner/tests_stream.js +++ b/lib/internal/test_runner/tests_stream.js @@ -62,13 +62,6 @@ class TestsStream extends Readable { }); } - bail(reason = ''){ - this[kEmitMessage]('test:bail', { - __proto__: null, - reason, - }); - } - getSkip(reason = undefined) { return { __proto__: null, skip: reason ?? true }; } diff --git a/test/parallel/test-runner-bail.js b/test/parallel/test-runner-bail.js index 109cb5dbcbac64..1fd6ebf48e9a39 100644 --- a/test/parallel/test-runner-bail.js +++ b/test/parallel/test-runner-bail.js @@ -32,7 +32,6 @@ 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/); From 6678a3b99539237012aaa78949c6a9b79006527c Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 29 Aug 2023 12:40:43 +0200 Subject: [PATCH 25/25] fix: removed tap bail and added snapshots --- lib/internal/test_runner/test.js | 9 ++--- .../fixtures/test-runner/output/bail-error.js | 13 ++++++++ .../test-runner/output/bail-error.snapshot | 32 ++++++++++++++++++ test/fixtures/test-runner/output/bail.js | 2 +- .../fixtures/test-runner/output/bail.snapshot | 33 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 2 ++ 6 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/test-runner/output/bail-error.js create mode 100644 test/fixtures/test-runner/output/bail-error.snapshot create mode 100644 test/fixtures/test-runner/output/bail.snapshot diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 43b7de41a54407..99dffe1a801ad5 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -503,7 +503,8 @@ class Test extends AsyncResource { this.endTime = hrtime(); this.passed = false; this.error = err; - if (bail) { + if (bail && !this.root.bailedOut) { + this.bailedOut = true; this.root.bailedOut = true; this.root.postRun(); } @@ -582,12 +583,6 @@ class Test extends AsyncResource { } async run() { - if (bailedOut) { - if (this.parent !== null) { - this.parent.postRun(); - } - return; - } if (this.parent !== null) { this.parent.activeSubtests++; } diff --git a/test/fixtures/test-runner/output/bail-error.js b/test/fixtures/test-runner/output/bail-error.js new file mode 100644 index 00000000000000..b4f785012bf5ba --- /dev/null +++ b/test/fixtures/test-runner/output/bail-error.js @@ -0,0 +1,13 @@ +// Flags: --test-bail --test-reporter=tap +'use strict'; +const common = require('../../../common'); +const assert = require('assert'); +const test = require('node:test'); + +test('keep error', (t) => { + assert.strictEqual(0, 1); +}); + +test('dont show', common.mustNotCall((t) => { + assert.strictEqual(0, 2); +})); diff --git a/test/fixtures/test-runner/output/bail-error.snapshot b/test/fixtures/test-runner/output/bail-error.snapshot new file mode 100644 index 00000000000000..edecd99aebe1bd --- /dev/null +++ b/test/fixtures/test-runner/output/bail-error.snapshot @@ -0,0 +1,32 @@ +TAP version 13 +not ok 1 - keep error + --- + duration_ms: * + location: '/test/fixtures/test-runner/output/bail-error.js:(LINE):1' + failureType: 'testCodeFailure' + error: |- + Expected values to be strictly equal: + + 0 !== 1 + + code: 'ERR_ASSERTION' + name: 'AssertionError' + expected: 1 + actual: 0 + operator: 'strictEqual' + stack: |- + * + * + * + * + * + ... +1..2 +# tests 2 +# suites 0 +# pass 0 +# fail 1 +# cancelled 1 +# skipped 0 +# todo 0 +# duration_ms * diff --git a/test/fixtures/test-runner/output/bail.js b/test/fixtures/test-runner/output/bail.js index 5c168bc03d04eb..232e2e7ea15a61 100644 --- a/test/fixtures/test-runner/output/bail.js +++ b/test/fixtures/test-runner/output/bail.js @@ -1,4 +1,4 @@ -// Flags: --test --test-bail +// Flags: --test-bail --test-reporter=tap 'use strict'; const common = require('../../../common'); const test = require('node:test'); diff --git a/test/fixtures/test-runner/output/bail.snapshot b/test/fixtures/test-runner/output/bail.snapshot new file mode 100644 index 00000000000000..6c81fe26a989aa --- /dev/null +++ b/test/fixtures/test-runner/output/bail.snapshot @@ -0,0 +1,33 @@ +TAP version 13 +# Subtest: nested + # Subtest: first + ok 1 - first + --- + duration_ms: * + ... + not ok 2 - second + --- + duration_ms: * + location: '/test/fixtures/test-runner/output/bail.js:(LINE):5' + failureType: 'cancelledByParent' + error: 'test did not finish before its parent and was cancelled' + code: 'ERR_TEST_FAILURE' + ... + 1..3 +not ok 1 - nested + --- + duration_ms: * + location: '/test/fixtures/test-runner/output/bail.js:(LINE):1' + failureType: 'subtestsFailed' + error: '2 subtests failed' + code: 'ERR_TEST_FAILURE' + ... +1..2 +# tests 5 +# suites 0 +# pass 1 +# fail 2 +# cancelled 2 +# skipped 0 +# todo 0 +# duration_ms * diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index ba84d9ea8e0430..2da8d2d1d53f36 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -88,6 +88,8 @@ const tests = [ replaceTestDuration, ), }, + { name: 'test-runner/output/bail.js', tty: true }, + { name: 'test-runner/output/bail-error.js', tty: true }, ] .filter(Boolean) .map(({ name, tty, transform }) => ({