mocha: Calling `done(undefined)` passes a test
When using done()
in an async test, if it’s called with an argument whose value is undefined
, the test is successfully passed.
Simple example:
it("should fail, but will pass", (done) => {
done(undefined);
});
This feels like incorrect behaviour because done
was explicitly called with an argument, which happened to not have a value. A test should only pass if done
is called without arguments (as per docs).
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 20 (13 by maintainers)
I haven’t checked the docs, but I would be 99% certain that Mocha does a falsy/truthy check on the first argument passed to done. Imagine you have a child process, if the cp exits with 0, it is successful, if it exits with 1 or greater, it’s usually not. 0 is of course, falsy.
Mocha is not checking for the amount of arguments passed to done, Mocha is checking whether the first argument passed to done is truthy. If it’s truthy, then the test fails. That is the expected behavior, TMK.
IMO the best thing to do would be to always log the first argument passed to the error-first callback, if arguments.length > 0, in the test results. That is the only change (besides the docs) that I see necessary. The pattern of truthy/falsy is so widespread in Node, it would be far more surprising to deviate from that than anything else.
in Node.js, we have this everywhere:
if(err){ cb(err); } else {
it’s a truthy/falsy check, and IMO Mocha should stay with that
On Sun, Nov 27, 2016 at 11:51 PM, Scott Santucci notifications@github.com wrote:
– Alexander Mills ORESoftware (inc.)