I have a simple case
.end(function(err, res) {
assert(err == null,'Error is not null => '+ err.stack);
the problem is of course, if err is null, the assert message will still get evaluated, so it will throw a new/preemptive error saying
Uncaught TypeError: Cannot read property 'stack' of null
I know assert is stable and not likely to change, but the feature I am looking for is somthing like this
assert(err == null, function(){
return 'Error is not null => '+ err.stack;
});
this of course means, that the callback will only be evaluated if the assertion fails
hope that makes sense, thanks!
Offhand, I'm unaware of any userland assertion testing library that has this feature, but it might be worth looking.
I suspect you already are doing something like this, but just in case, a simple workaround for your issue might look something like this:
const msg = err ? `Error is not null => ${err.stack}` : '';
assert(err == null, msg);
Or:
if (err) {
assert.fail(null, null, `Error is not null => ${err.stack}`);
}
Yeah that will, I was kind of curious to see how useful this callback feature might be, probably not that useful and your suggestion should work, thanks
Node.js assertion module is not meant for general purpose use. Unless Node.js requires this feature, it wouldn't be implemented and as of now we don't have a pressing usecase.
@ORESoftware Since 81f2c9317aa9f9b4e69ed7a60dbecd8affd462a8 landed, you don't have to do err.stack anymore. By default, whenever an Error object is written out, it will print its stack too.
Do note that the commit hasn't landed in node 5.x and will _not_ land until node 6.x.
Closing as the assert module is locked. Thanks!
@thefourtheye why is the assert module not meant for general purpose use? I've tried using it on small projects instead of things Shouldjs and have found it meets my needs well.
Basically the same question as this one on Stack Overflow
@mheiber https://github.com/nodejs/node/issues/4532 and other issues allude to the reason the documentation recommends against using assert for unit testing: There are edge case bugs (or at least certainly surprises) and missing features.
A little more context: Knowing what we now know, if we were designing/building Node.js core all over again, the assert module would either not exist in Node.js or else consist of far fewer functions--quite possibly just assert() (which is currently an alias for assert.ok()).
The reasons for this, at least from my perspective are:
assert could easily be done in userlandThere's additional context that others may choose to add here or not (such as why, all things being equal, we would favor keeping core small and doing things in userland). But that's the so-called 30,000 foot view.
Since assert has been in Node.js for a long time and a lot of the ecosystem depends on it, we are unlikely (at least as best as I can tell at the current time) to ever remove assert.throws() and friends. It would break too much stuff. But we can discourage people from using assert and encourage them to use userland modules that are maintained by people who care deeply about them and who aggressively fix edge-case bugs and who add cool new features when it makes sense. So that's what that's all about.
True, if you're doing straightforward assertions with simple cases, assert probably will meet your needs. But if you ever outgrow assert, you'll be better off with chai or whatever. So we encourage people to start there. It's better for them (usually) and better for us (usually).
I hope this is helpful and answers your question.
@trott this answer rules, thank you
@mheiber Heh, thanks. If you wish, consider upvoting the answer's twin at http://stackoverflow.com/a/39284536/436641 to increase its visibility there.
Most helpful comment
@mheiber https://github.com/nodejs/node/issues/4532 and other issues allude to the reason the documentation recommends against using
assertfor unit testing: There are edge case bugs (or at least certainly surprises) and missing features.A little more context: Knowing what we now know, if we were designing/building Node.js core all over again, the
assertmodule would either not exist in Node.js or else consist of far fewer functions--quite possibly justassert()(which is currently an alias forassert.ok()).The reasons for this, at least from my perspective are:
assertcould easily be done in userlandThere's additional context that others may choose to add here or not (such as why, all things being equal, we would favor keeping core small and doing things in userland). But that's the so-called 30,000 foot view.
Since
asserthas been in Node.js for a long time and a lot of the ecosystem depends on it, we are unlikely (at least as best as I can tell at the current time) to ever removeassert.throws()and friends. It would break too much stuff. But we can discourage people from usingassertand encourage them to use userland modules that are maintained by people who care deeply about them and who aggressively fix edge-case bugs and who add cool new features when it makes sense. So that's what that's all about.True, if you're doing straightforward assertions with simple cases,
assertprobably will meet your needs. But if you ever outgrowassert, you'll be better off withchaior whatever. So we encourage people to start there. It's better for them (usually) and better for us (usually).I hope this is helpful and answers your question.