Repository navigation
Order of nested vs outer afterEach hooks is unexpected and inconsistent with after #51671
Copy link
Copy link
Closed
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.test_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
Description
Activity
- addedtest_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on Feb 7, 2024 I agree subtest
afterandafterEachshould be called before parent @nodejs/test_runner.
I couldnt reproduce withtestthough.This works fine:
'use strict'; require('../common'); const assert = require('node:assert'); const { test, afterEach, after } = require('node:test'); let afterEachRacing = true; let afterRacing = true; test('parent', () => { afterEach(() => afterEachRacing = false); after(() => afterRacing = false); test('child', () => { afterEach(() => assert.ok(afterEachRacing)); after(() => assert.ok(afterRacing)); test(() => { }); }); });
this fails:
'use strict'; require('../common'); const assert = require('node:assert'); const { describe, it, afterEach, after } = require('node:test'); let afterEachRacing = true; let afterRacing = true; describe('parent', () => { afterEach(() => afterEachRacing = false); after(() => afterRacing = false); describe('child', () => { afterEach(() => assert.ok(afterEachRacing)); after(() => assert.ok(afterRacing)); it(() => { }); }); });
I meant that
test('parent', async (t) => { t.afterEach((c) => console.log('parent after each', c.name)); t.after((c) => console.log('parent after', c.name)); await t.test('child', async (t) => { t.afterEach((c) => console.log('child after each', c.name)); t.after((c) => console.log('child after', c.name)); await t.test('works', () => {}); }); });
produces
parent after each works child after each works parent after each child child after child parent after parentinstead of
child after each works parent after each works parent after each child child after child parent after parentso the parent's afterEach for
worksruns before the child's.- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Mar 28, 2024 - added a commit that references this issue
on Mar 28, 2024 - added a commit that references this issue
on Mar 30, 2024 - added 2 commits that reference this issue
on May 2, 2024
Metadata
Metadata
Assignees
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.test_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
Version
21.6.1
Platform
darwin
Subsystem
test runner
What steps will reproduce the bug?
Create a file called
hooks.mjscontaining: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?
As with
mocha,qunit,jest,vitest, and probably others, a child suite'safterEachhooks should be called before those of parent suites, not after them. Also, the execution order of parent/childafterEachhooks should match the order of parent/childafterhooks.Currently,
afterhooks correctly run child-first, butafterEachhooks run incorrectly parent-first.What do you see instead?
Additional information
This also reproduces with the
test()/subtest API -- a test'safterEachhook will run before a subtest'safterEachhook.