Skip to content

afterEach hook not executed when test fails #45192

Description

@rochdev

Version

v19.0.0

Platform

Darwin Rochs-MBP.localdomain 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

Subsystem

node:test

What steps will reproduce the bug?

When a test fails, the afterEach hook is not executed.

const { test } = require('node:test')

test('suite', async t => {
  t.beforeEach(() => {
    console.log('beforeEach')
  })

  t.afterEach(() => {
    console.log('afterEach') // this is never executed
  })

  await t.test('test', t => {
    throw new Error('boom')
  })
})

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

This is always reproducible when the test fails.

What is the expected behavior?

The afterEach hook should always be executed, especially since it often serves as a way to clean up resources.

What do you see instead?

Only the code for beforeEach is executed.

Additional information

This also happens with the describe/it syntax.

Activity

  1. added
    good first issueIssues that are suitable for first-time contributors.
    test_runnerIssues and PRs related to the test runner subsystem.
    and removed
    good first issueIssues that are suitable for first-time contributors.
    on Oct 26, 2022
  2. MrJithil commented on Oct 27, 2022

    @MrJithil
    Member

    I can work on this.

  3. MoLow commented on Oct 27, 2022

    @MoLow
    Member

    @MrJithil go for it!

  4. MrJithil commented on Oct 27, 2022

    @MrJithil
    Member

    @MrJithil go for it!

    Done the initial version. Will wait for the suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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