Skip to content

afterEach hook not executed when test fails #45192

Closed
@rochdev

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.

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