Skip to content

beforeEach/afterEach hooks not executing for nested tests #47643

@rochdev

Description

@rochdev

Version

v20.0.0

Platform

Darwin Rochs-MBP.localdomain 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000 arm64

Subsystem

node:test

What steps will reproduce the bug?

const { describe, beforeEach, it } = require('node:test')

describe('Root', () => {
  beforeEach(() => {
    console.log('test') // only prints when the Nested context is entered
  })

  describe('Nested', () => {
    it('should run beforeEach in first test', () => {
      // always pass
    })

    it('should run beforeEach in second test', () => {
      // always pass
    })

    it('should run beforeEach in third test', () => {
      // always pass
    })
  })
})

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

Always.

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

beforeEach and afterEach should be running before every test even when nested, not when the nested suite starts running.

What do you see instead?

beforeEach and afterEach only run once for nested test suites.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssues that are suitable for first-time contributors.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