Repository navigation
test_runner: allow to force exit after all tests finished #49925
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.test_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on Sep 28, 2023 cc @nodejs/test_runner
Do we need to do anything? Wouldn't a
process.exit()call inafteralready does what you want?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.
in
node:testthere 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.Do we need to do anything? Wouldn't a
process.exit()call inafteralready does what you want?process.exitwill 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)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...
Reacted by Erick WendelUnfortunately, 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...
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?
Reacted by Erick Wendel, Allain Lalonde and James PrevettReacted by Moshe AtlowI 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.
Reacted by Jarrod ConnollyReacted by Alejandro Barrera Aponteanother option is to add on the
TestContexta getter with the pending tests count, this wat on theafter/afterEachhook it will be possible to run cleanup code depending on that getterI 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.)Reacted by Moshe Atlow and Chemi Atlow6 remaining items
- added a commit that references this issue
on Mar 11, 2024 - added a commit that references this issue
on Mar 13, 2024 - added a commit that references this issue
on May 2, 2024 - added a commit that references this issue
on May 23, 2024 - added 2 commits that reference this issue
on Oct 8, 2024 To summarize that PR for folk who find this issue looking for "how do I do this", a
--test-force-exitflag was added (but at least for my problem does not seem to exit the tests after all tests have pased. There was a danglingreadline.createInterfacethat 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?
@Pomax yes, thank you
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:
mochahas--exitflagjesthas--forceExitWhat alternatives have you considered?
No response