node icon indicating copy to clipboard operation
node copied to clipboard

node:test beforeEach and afterEach generate MaxListenersExceededWarning

Open lg250137 opened this issue 1 year ago • 0 comments

Version

v20.3.0

Platform

No response

Subsystem

node:test

What steps will reproduce the bug?

import {beforeEach, test} from "node:test"
beforeEach(() => {})
for (let i=1; i<=11; ++i) test(`${i}`, () => {})

Run tests with node --test

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

Consistently with 11 or more tests that share a beforeEach or afterEach hook. This does not occur if each test has its own after hook, e.g.: test(`${i}`, (t) => t.after(() => {}))

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

No warning should be emitted when running the tests. It is reasonable to have more than 10 unit tests that share a beforeEach and/or afterEach hook, and explicitly using events.setMaxListeners() in the unit tests should not be necessary to avoid the warning.

What do you see instead?

$ node --trace-warnings --test
(node:11354) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
    at [kNewListener] (node:internal/event_target:539:17)
    at [kNewListener] (node:internal/abort_controller:235:24)
    at EventTarget.addEventListener (node:internal/event_target:650:23)
    at eventTargetAgnosticAddListener (node:events:1024:13)
    at node:events:981:5
    at new Promise (<anonymous>)
    at once (node:events:964:10)
    at stopTest (node:internal/test_runner/test:78:12)
    at TestHook.run (node:internal/test_runner/test:547:27)
    at TestHook.run (node:internal/test_runner/test:748:18)

Additional information

No response

lg250137 avatar Jun 16 '23 19:06 lg250137