From 6141d5e7c6003bf58d485c4c562d27ae3dd0e2a4 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 16 Mar 2024 12:12:44 -0400 Subject: [PATCH] test_runner: skip each hooks for skipped tests When a test is skipped, the corresponding beforeEach and afterEach hooks should also be skipped. Fixes: https://github.com/nodejs/node/issues/52112 --- lib/internal/test_runner/test.js | 4 ++-- .../test-runner/output/skip-each-hooks.js | 21 +++++++++++++++++++ .../output/skip-each-hooks.snapshot | 11 ++++++++++ test/parallel/test-runner-output.mjs | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/test-runner/output/skip-each-hooks.js create mode 100644 test/fixtures/test-runner/output/skip-each-hooks.snapshot diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index d57c1d6d12610e..e5877a1d5ec9e4 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -666,7 +666,7 @@ class Test extends AsyncResource { } }; const afterEach = runOnce(async () => { - if (this.parent?.hooks.afterEach.length > 0) { + if (this.parent?.hooks.afterEach.length > 0 && !this.skipped) { await this.parent.runHook('afterEach', hookArgs); } }); @@ -678,7 +678,7 @@ class Test extends AsyncResource { // This hook usually runs immediately, we need to wait for it to finish await this.parent.runHook('before', this.parent.getRunArgs()); } - if (this.parent?.hooks.beforeEach.length > 0) { + if (this.parent?.hooks.beforeEach.length > 0 && !this.skipped) { await this.parent.runHook('beforeEach', hookArgs); } stopPromise = stopTest(this.timeout, this.signal); diff --git a/test/fixtures/test-runner/output/skip-each-hooks.js b/test/fixtures/test-runner/output/skip-each-hooks.js new file mode 100644 index 00000000000000..f4fbb2d23fffa3 --- /dev/null +++ b/test/fixtures/test-runner/output/skip-each-hooks.js @@ -0,0 +1,21 @@ +// Flags: --test-reporter=spec +'use strict'; +const { after, afterEach, before, beforeEach, test } = require('node:test'); + +before(() => { + console.log('BEFORE'); +}); + +beforeEach(() => { + console.log('BEFORE EACH'); +}); + +after(() => { + console.log('AFTER'); +}); + +afterEach(() => { + console.log('AFTER EACH'); +}); + +test('skipped test', { skip: true }); diff --git a/test/fixtures/test-runner/output/skip-each-hooks.snapshot b/test/fixtures/test-runner/output/skip-each-hooks.snapshot new file mode 100644 index 00000000000000..e92376101dac1d --- /dev/null +++ b/test/fixtures/test-runner/output/skip-each-hooks.snapshot @@ -0,0 +1,11 @@ +BEFORE +AFTER +﹣ skipped test (*ms) # SKIP +ℹ tests 1 +ℹ suites 0 +ℹ pass 0 +ℹ fail 0 +ℹ cancelled 0 +ℹ skipped 1 +ℹ todo 0 +ℹ duration_ms * diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 77a3f4e2a0263b..159f66751ae2b7 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -96,6 +96,7 @@ const tests = [ { name: 'test-runner/output/eval_tap.js' }, { name: 'test-runner/output/hooks.js' }, { name: 'test-runner/output/hooks_spec_reporter.js', transform: specTransform }, + { name: 'test-runner/output/skip-each-hooks.js', transform: specTransform }, { name: 'test-runner/output/timeout_in_before_each_should_not_affect_further_tests.js' }, { name: 'test-runner/output/hooks-with-no-global-test.js' }, { name: 'test-runner/output/global-hooks-with-no-tests.js' },