From 561b8558ef65c311394cee933d23eb0a53a5066d Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Tue, 25 Jul 2023 11:07:10 +0300 Subject: [PATCH] test_runner: fix global after not failing the tests --- lib/internal/test_runner/test.js | 8 +++ .../global-after-should-fail-the-test.js | 10 ++++ .../output/hooks-with-no-global-test.js | 54 +++++++++---------- test/parallel/test-runner-output.mjs | 1 + 4 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 test/fixtures/test-runner/output/global-after-should-fail-the-test.js diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 9c76a371462057..5780158cc51fee 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -655,6 +655,14 @@ class Test extends AsyncResource { this.parent.processReadySubtestRange(false); this.parent.processPendingSubtests(); } else if (!this.reported) { + if (!this.passed && failed === 0 && this.error) { + this.reporter.fail(this.nesting, kFilename, this.testNumber, this.name, { + __proto__: null, + duration_ms: this.#duration(), + error: this.error + }, undefined); + } + this.reported = true; this.reporter.plan(this.nesting, kFilename, this.root.harness.counters.topLevel); diff --git a/test/fixtures/test-runner/output/global-after-should-fail-the-test.js b/test/fixtures/test-runner/output/global-after-should-fail-the-test.js new file mode 100644 index 00000000000000..e2ad4c815b7fcd --- /dev/null +++ b/test/fixtures/test-runner/output/global-after-should-fail-the-test.js @@ -0,0 +1,10 @@ +'use strict'; +const { it, after } = require('node:test'); + +after(() => { + throw new Error('this should fail the test') +}); + +it('this is a test', () => { + console.log('this is a test') +}); diff --git a/test/fixtures/test-runner/output/hooks-with-no-global-test.js b/test/fixtures/test-runner/output/hooks-with-no-global-test.js index 844aa6ff3c2d59..ea01463fd6cc1f 100644 --- a/test/fixtures/test-runner/output/hooks-with-no-global-test.js +++ b/test/fixtures/test-runner/output/hooks-with-no-global-test.js @@ -9,42 +9,36 @@ before(() => testArr.push('global before')); after(() => { testArr.push('global after'); - try { - assert.deepStrictEqual(testArr, [ - 'global before', - 'describe before', + assert.deepStrictEqual(testArr, [ + 'global before', + 'describe before', - 'describe beforeEach', - 'describe it 1', - 'describe afterEach', + 'describe beforeEach', + 'describe it 1', + 'describe afterEach', - 'describe beforeEach', - 'describe test 2', - 'describe afterEach', + 'describe beforeEach', + 'describe test 2', + 'describe afterEach', - 'describe nested before', + 'describe nested before', - 'describe beforeEach', - 'describe nested beforeEach', - 'describe nested it 1', - 'describe afterEach', - 'describe nested afterEach', + 'describe beforeEach', + 'describe nested beforeEach', + 'describe nested it 1', + 'describe afterEach', + 'describe nested afterEach', - 'describe beforeEach', - 'describe nested beforeEach', - 'describe nested test 2', - 'describe afterEach', - 'describe nested afterEach', + 'describe beforeEach', + 'describe nested beforeEach', + 'describe nested test 2', + 'describe afterEach', + 'describe nested afterEach', - 'describe nested after', - 'describe after', - 'global after', - ]); - } catch (e) { - // TODO(rluvaton): remove the try catch after #48867 is fixed - console.error(e); - process.exit(1); - } + 'describe nested after', + 'describe after', + 'global after', + ]); }); describe('describe hooks with no global tests', () => { diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 76c511117ea091..df753fd78029be 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -37,6 +37,7 @@ const tests = [ { name: 'test-runner/output/describe_nested.js' }, { name: 'test-runner/output/hooks.js' }, { name: 'test-runner/output/hooks-with-no-global-test.js' }, + { name: 'test-runner/output/global-after-should-fail-the-test.js' }, { name: 'test-runner/output/no_refs.js' }, { name: 'test-runner/output/no_tests.js' }, { name: 'test-runner/output/only_tests.js' },