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: Test stops recognizing exceptions once done has been called #4202
Comments
Yes that seems like a bug. Can replicate via done();
equal(true, false);
done(); It should throw a failure but it passes, not good. Guessing initial |
@craigtaub Your example is not a problem. writing that JS another way: done();
throw new Error();
done(); You wouldn't expect the second @feckertson Can you show that your code does not do something like the above? If there's an exception thrown before the second call to Regardless: if Mocha is eating errors, please provide a minimal, reproducible case. |
@boneskull Isn't the point of a unit testing framework to help code authors discover coding mistakes? My simple example at the beginning has a an assert after a call to done which is one type of coding mistake, Currently, such a mistake can cause a test to pass for the wrong reason. I gave the actual coding error which led to this issue under the "Additional Information" heading. One path through that code leads to a duplicate call to the callback function. There is a test that ought to expose that mistake by either failing an assert or by generating a duplicate call to done error, but neither error occurs. It seems to me like an error should occur if any code is executed by a test after done is called. |
I misunderstood @craigtaub; I thought he was saying that the code doesn't throw a "multiple calls to done()" exception when it should. That should throw. This is the circumstance where it('should fail', function(done) {
setTimeout(() => {
done();
throw new Error();
});
}); I haven't had time to bisect, but it may be a regression introduced by a fix for #4030.
Uh... have you ever written a bug? Please keep the entitlement out of our issue tracker; we don't work for you. |
cc @juergba; the code here was most recently changed when fixing an issue with |
#4030 was released in v7.0.0, so probably this is an older issue. I will have a look. |
const assert = require('assert');
it('problematic test', function(done) {
done();
console.log('after done');
assert(false);
}); Above test does not run correctly neither in Mocha v5.2.0, nor v7.2.0, nor in v8.0.1. Similar test cases with real async functions are handled by Mocha's |
Confirmed that that last code snippet (#4202 (comment)) doesn't fail as expected in Mocha 10.2.0: const assert = require("assert");
it("problematic test", function (done) {
done();
console.log("after done");
assert(false);
});
...while the earlier it("should fail", function (done) {
setTimeout(() => {
done();
throw new Error();
});
});
Fun! |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
Mocha stops recognizing exceptions once done has been called.
Steps to Reproduce
Find a complete example in git, but an outline is given below.
Source
Test
Expected behavior: We expect the test to fail because of the failed expect assertion.
Actual behavior: The test passes, but if the two lines are reversed the test fails.
Reproduces how often: 100%
Versions
Multiple including mocha 5.2.0/7.1.0, node 8.10.0/10.16.0 (64-bit) on Windows cmd and Linux bash.
Additional Information
This came to our attention when faulty code similar to the following was submitted for review. Mocha failed to indicate there was a problem with the code even though Stryker reported 100% mutation coverage.
The expect fails during the extraneous call to callback since the statusCode is 200. Consequently no duplicate call to done occurs. If it weren't for the assert failure mocha would catch the duplicate call to done.
The text was updated successfully, but these errors were encountered: