Skip to content

Commit

Permalink
test_runner: skip each hooks for skipped tests
Browse files Browse the repository at this point in the history
When a test is skipped, the corresponding beforeEach and afterEach
hooks should also be skipped.

Fixes: nodejs#52112
  • Loading branch information
cjihrig committed Mar 16, 2024
1 parent fec7e50 commit 6141d5e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/test-runner/output/skip-each-hooks.js
Original file line number Diff line number Diff line change
@@ -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 });
11 changes: 11 additions & 0 deletions test/fixtures/test-runner/output/skip-each-hooks.snapshot
Original file line number Diff line number Diff line change
@@ -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 *
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down

0 comments on commit 6141d5e

Please sign in to comment.