Skip to content

test_runner: allow to force exit after all tests finished #49925

Description

@rluvaton

What is the problem this feature will solve?

if you have some resource that you don't control blocks the process from exiting (socket/timer/etc) the tests will never finish

having timeout in the run options marking the test as failed

What is the feature you are proposing to solve the problem?

allow to specify force exit option that after all tests finished will exit

similar solutions in other frameworks:

What alternatives have you considered?

No response

Activity

  1. added
    feature requestIssues requesting new Node.js features.
    test_runnerIssues and PRs related to the test runner subsystem.
    on Sep 28, 2023
  2. rluvaton commented on Sep 28, 2023

    @rluvaton
    MemberAuthor

    cc @nodejs/test_runner

  3. aduh95 commented on Sep 28, 2023

    @aduh95
    Contributor

    Do we need to do anything? Wouldn't a process.exit() call in after already does what you want?

  4. cjihrig commented on Sep 28, 2023

    @cjihrig
    Contributor

    I don't quite understand the use case.

    if you have some resource that you don't control

    It's your test suite, so in theory you have some level of control over all of the resources. It sounds like you just aren't cleaning up after yourself properly.

  5. MoLow commented on Sep 28, 2023

    @MoLow
    Member

    in node:test there is no such event as "after all tests finished".

    this is totally valid:

    await setTimeout(10 * 60 * 1000) // sleep for ten minutes
    test('run a test after ten minutes', () => {});

    and there is no way to know a test is going to be enqueued besides waiting for event loop to drain using process.on('beforeExit') - that is what we do already.

  6. rluvaton commented on Sep 28, 2023

    @rluvaton
    MemberAuthor

    Do we need to do anything? Wouldn't a process.exit() call in after already does what you want?

    process.exit will mess with the reporting as the report will not have enough time to reach the parent process (waiting for a second and then exiting solves this but I don't think it's a solution)

  7. rluvaton commented on Sep 28, 2023

    @rluvaton
    MemberAuthor

    I don't quite understand the use case.

    if you have some resource that you don't control

    It's your test suite, so in theory you have some level of control over all of the resources. It sounds like you just aren't cleaning up after yourself properly.

    Unfortunately, the use case is some dependency of dependency that I'm using is not cleaning up...

  8. atlowChemi commented on Sep 28, 2023

    @atlowChemi
    Member

    Unfortunately, the use case is some dependency of dependency that I'm using is not cleaning up...

    @rluvaton that sounds to me like a bug within the dependency, which should be fixed there...

    In addition, as Moshe said, the test runner can't tell if all tests completed, as we do not build the test tree before executing the tests, which means adding such an implementation would also make executing the tests that much slower...

  9. benjamingr commented on Sep 28, 2023

    @benjamingr
    Member

    I think this is reasonable and test-runners typically implement this because it's common enough, I'm also not sure we can implement this reasonably with our current design.

    We can however (with a flag) exit when no more tests are enqueued within a microtick after all already enqueued tests were run which should work well in practice maybe?

  10. cjihrig commented on Sep 28, 2023

    @cjihrig
    Contributor

    I disagree. I don't think we should implement this, if for no other reason than I don't think it's worth another CLI flag. You're literally saying you have no control over your own application if you can't make it stop.

  11. MoLow commented on Sep 28, 2023

    @MoLow
    Member

    another option is to add on the TestContext a getter with the pending tests count, this wat on the after/afterEach hook it will be possible to run cleanup code depending on that getter

  12. cjihrig commented on Sep 28, 2023

    @cjihrig
    Contributor

    I don't mind that idea, but if I understand #49925 (comment) correctly, the problem is that they are not able to perform any cleanup.

    (I'm curious to hear more about this by the way because if the issue is that a handle is holding the event loop open, you should be able to use something like process._getActiveHandles() to find it and likely force close it. It's not ideal, but it sounds like neither is the current situation.)

  13. 6 remaining items

  14. cjihrig commented on Mar 10, 2024

    @cjihrig
    Contributor
  15. added a commit that references this issue on Mar 11, 2024
  16. added a commit that references this issue on May 2, 2024
  17. moved this from Awaiting Triage to Done in Node.js feature requestson Jun 28, 2024
  18. Pomax commented on Sep 12, 2025

    @Pomax

    To summarize that PR for folk who find this issue looking for "how do I do this", a --test-force-exit flag was added (but at least for my problem does not seem to exit the tests after all tests have pased. There was a dangling readline.createInterface that needed closing).

    The "Test Runner" API docs, which covers the testing flags, do not appear to have been updated accordingly though. Does that need a follow-up PR @cjihrig?

  19. rluvaton commented on Oct 1, 2025

    @rluvaton
    MemberAuthor

    @Pomax yes, thank you

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

    feature requestIssues requesting new Node.js features.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

      Sponsor
      SponsoredKunjungi sekarang
      Promo