-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Bug: --timeout 0 with unref'd timers causes process exit #3817
Comments
@boneskull, look at this... |
Try to remove all
|
@juergba - I should note that I can't remove the Also, if I run with the flag --no-timeout , mocha skips the tests anyway.... so it's an issue with --no-timeout too.
|
Do your tests get ignored outside of webstorm when using cc @segrey |
Ah, looks like you answered that. |
The problem is with |
Seems like it works in v5.2.0 when using |
(@segrey, nevermind, this doesn't have anything to do with webstorm) |
So it looks like because timeouts are disabled within Mocha, and The it('just kind of stops', function(done) {
setTimeout(done, 10).unref();
}); |
The bad news is, I'm not sure how to fix this other than suggest you use |
@boneskull ... but why does setting --timeout 0 twice make it work again? |
that's a separate bug. it causes the timeout to become |
(somebody please make a ticket for weirdness when the CLI parser thinks something like |
another workaround, if you can get away with removing |
We have a bunch of tests that make http calls but the socket is unrefd in a low level library... these calls die. I cannot easily change this... and i would have to modify 100s of tests to wrap them in something refd. The workaround with the doubled timeout will be my preference... until the Cli is fixed. |
@boneskull - I have played with the code a little to figure out what options are available. The underlying issue is that Mocha itself does not prevent exits when test cases are incomplete. It is reasonable for a test to call code that unref's timers, etc. so it is reasonable for mocha to hold the event loop active during tests. I added a hold on to the event loop for the duration of a Mocha run using the code:
as seen in this change: rolfl@bd4a611#diff-aa849a970cef551664c12f04a4209f6fR833 it works "well", and I also added the "trivial" test case and added it to the test suites. The issue is in the Before I try to go further, is the adding of the event-loop holding timeout a viable option, and if it is, is there a way to get the mocha unit tests to pass with the sinon fakery? |
For this special |
@juergba - that's true, I could. On the other hand, I would have to add it to all files/tests because it is common when in the IDE doing TDD to set up a test-case, and ru just that one test case. The before/after would have to be called when running any single test too. It's not a practical solution. Again, the bug is that mocha is not waiting for a test to complete before the mocha exits. The fact that the code-under-test is not locking the event loop is good for that code. |
@rolfl There is no fix (that I'm aware of) other than using |
Adding a dummy timeout seems like a bandaid on the "real" fix. I am not sure what the "real" fix is. Mocha does not consume Sinon's fake timers; these are disabled by default by |
This is incorrect; you can do this: // hack.js
let t;
before(function() {
t = setTimeout(() => {}, 1e9);
});
after(function() {
clearTimeout(t);
}); run via $ mocha --file hack.js path/to/tests |
I have an idea to address this. While I have a feeling this is actually "expected behavior" in Node.js, users shouldn't have to deal with it. |
…3817 Signed-off-by: Christopher Hiller <[email protected]>
…3817 Signed-off-by: Christopher Hiller <[email protected]>
Ref: #3825 |
…3817 Signed-off-by: Christopher Hiller <[email protected]>
Released in |
Background context - SSCCE to follow. Bear with me, this behaviour is somewhat bizarre, so explaining it is complicated....
When using WebStorm as an IDE, it can run the mocha tests in "run" mode or "debug" mode. If the tests are constructed in a particular way (specifically - using promises) In "run" mode, everything works fine. In debug mode, all the tests are ignored.... in WebStorm, it looks like:
It took me a lot of trial-and-error to identify the "root cause", but it boils down to the
--timeout 0
that webstorm adds when doing thenode --inspect-brk
part of debugging. The timeout 0 is added to allow breakpoints to pause the code without causing the tests to time out.The problem can be explained with the following test case:
Note, all the test does, is wait 10ms, and then succeed.
There are 2 key features in the above....
it(...)
test.unref()
on thesetTimeout()
If you run the above normally, it succeeds
node ./node_modules/mocha/bin/_mocha --ui bdd tests/sscce.spec.js
:If you add
--timeout 0
it completes successfully, but does not actually run the test:node ./node_modules/mocha/bin/_mocha --timeout 0 --ui bdd tests/sscce.spec.js && echo pass
gives:The same is true (it also fails) if you run in debug mode (
--inspect
).BUT, if you add a second
--timeout 0
it will then work correctly:node ./node_modules/mocha/bin/_mocha --timeout 0 --timeout 0 --ui bdd tests/sscce.spec.js && echo pass
My guess is that the code that waits for Promise-based tests to complete is not actively waiting on the promise, so the node system discovers there are no events to prevent system exit, so it exits...
But, for the life of me, I don't understand why adding multiple
--timeout 0
flags makes it work.....Oh, and, if you use WebStorm, you end up having to add 2
--timeout 0
flags in your configuration, so that when you run the tests it has 2 timeouts, and when you debug, it has 3.Edited to add versions:
The text was updated successfully, but these errors were encountered: