Skip to content

Order of nested vs outer afterEach hooks is unexpected and inconsistent with after #51671

Closed
@bendemboski

Description

@bendemboski

Version

21.6.1

Platform

darwin

Subsystem

test runner

What steps will reproduce the bug?

Create a file called hooks.mjs containing:

import { describe, afterEach, after, it } from 'node:test';

describe('parent', () => {
  afterEach(() => console.log('parent after each'));
  after(() => console.log('parent after'));

  describe('child', () => {
    afterEach(() => console.log('child after each'));
    after(() => console.log('child after'));

    it('works', () => {});
  });
});

and run node --test hooks.mjs.

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

Always

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

child after each
parent after each
child after
parent after
<test results>

As with mocha, qunit, jest, vitest, and probably others, a child suite's afterEach hooks should be called before those of parent suites, not after them. Also, the execution order of parent/child afterEach hooks should match the order of parent/child after hooks.

Currently, after hooks correctly run child-first, but afterEach hooks run incorrectly parent-first.

What do you see instead?

parent after each
child after each
child after
parent after
<test results>

Additional information

This also reproduces with the test()/subtest API -- a test's afterEach hook will run before a subtest's afterEach hook.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.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