Sitelet https://github.com/nodejs/node/issues/48475
Skip to content

node:test beforeEach and afterEach generate MaxListenersExceededWarning #48475

Description

@lg250137

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

Activity

  1. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Jun 20, 2023
  2. rluvaton commented on Jul 25, 2023

    @rluvaton
    Member

    will fix it now

  3. Prateek462003 commented on Jul 25, 2023

    @Prateek462003

    Can i take a look at it ?

  4. rluvaton commented on Jul 25, 2023

    @rluvaton
    Member

    Can i take a look at it ?

    Sorry, missed that. maybe next time?

  5. added a commit that references this issue on Jul 29, 2023
    272ff66
  6. added a commit that references this issue on Jul 31, 2023
    7a33f2b
  7. added 2 commits that reference this issue on Aug 14, 2023
    9f7a545
    abd84ad
  8. added a commit that references this issue on Aug 14, 2023
    dec69e9
  9. added 2 commits that reference this issue on Aug 15, 2023
    67859bd
    5b90126
  10. added a commit that references this issue on Sep 4, 2023
    b91c906
  11. added a commit that references this issue on Sep 10, 2023
    c2f1830
  12. added a commit that references this issue on Nov 27, 2023
    67d7fad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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