Skip to content

test_runner: before hook runs even if it's not followed by any test #53202

Closed
@urugator

Description

@urugator

Version

v20.13.1

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

test_runner

What steps will reproduce the bug?

import { before, describe, test } from 'node:test';

describe('suite1', (s) => {
  before(async () => {
    console.log(`${s.name} before`);
  });

  test('test1', async (t) => {
    console.log(`${s.name} ${t.name}`);
  });

  test('test2', async (t) => {
    console.log(`${s.name} ${t.name}`);
  });
});

Execute with:
node --test --test-name-pattern="nonsence"

How often does it reproduce? Is there a required condition?

Always. No.

What is the expected behavior? Why is that the expected behavior?

before hook shouldn't run if it's not followed by any test. There is no practial or logical reason to run before hook alone, it wastes time and resources.

What do you see instead?

suite1 before
▶ suite1
  ﹣ test1 (2.2042ms) # test name does not match pattern
  ﹣ test2 (0.3612ms) # test name does not match pattern
▶ suite1 (8.3937ms)
ℹ tests 2
ℹ suites 1
ℹ pass 0
ℹ fail 0
ℹ cancelled 0
ℹ skipped 2
ℹ todo 0
ℹ duration_ms 141.0891

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions