Skip to content

test runner runs beforeEach or test case before "before hook" finished  #52764

Closed
@ert78gb

Description

@ert78gb

Version

v22.0.0

Platform

Darwin Kernel Version 23.4.0

Subsystem

test_runner

What steps will reproduce the bug?

run the following code with node 22.0.0.

import assert from "node:assert/strict";
import test from "node:test";
import { setTimeout } from "node:timers/promises";

await test("test suite", async (t) => {
  t.before(async () => {
    console.log("before start");
    await setTimeout(100);
    console.log("before end");
  });

  t.beforeEach(async () => {
    console.log("beforeEach start");
    await setTimeout(100);
    console.log("beforeEach end");
  });

  t.afterEach(async () => {
    console.log("afterEach start");
    await setTimeout(100);
    console.log("afterEach end");
  });

  t.after(async () => {
    console.log("after start");
    await setTimeout(100);
    console.log("after end");
  });

  await t.test("test case", async () => {
    console.log("test case start");
    await setTimeout(100);
    assert.ok(true);
    console.log("test case end");
  });
});

the expected output is

before start
before end
beforeEach start
beforeEach end
test case start
test case end
afterEach start
afterEach end
after start
after end

instead of

before start
beforeEach start << "beforEach" strarts before "before end"
before end
beforeEach end
test case start
test case end
afterEach start
afterEach end
after start
after end

If the hooks are not async then the order is correct.

In earlier node version the await keyword helped before the hooks but in node 22.0.0 it doesn't work.

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

always

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

the expected console log is

before start
before end
beforeEach start
beforeEach end
test case start
test case end
afterEach start
afterEach end
after start
after end

What do you see instead?

before start
beforeEach start << "beforEach" strarts before "before end"
before end
beforeEach end
test case start
test case end
afterEach start
afterEach end
after start
after end

Additional information

if you need git repo https://github.com/ert78gb/node-22-test-runner-async-hooks

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