nodeunit
nodeunit copied to clipboard
Exceptions in test code are missed
Exceptions that occur inside test functions are completely missed. This creates issues like test failing but makes it completely unclear where the failure occured. The exceptions should be catched and a stack trace should be printed for any exception that happens inside a test method.
We already wrap tests in try/catch, if you're using async methods inside the tests then the actual exception may appear elsewhere. Please submit a test case which demonstrates your problem and I'll see if there's anything we can do to improve the reporting.
Hi, so this actually happens when an exception is thrown asynchronously like so:
exports.asyncThrow = function (t) {
setImmediate(function() { throw new Error('thrown asynchronously');});
}
Running the following test results in:
└─[0] <git:(master✱✈)> ./bin/nodeunit ./test/foo.test.js
foo.test.js
FAILURES: Undone tests (or their setups/teardowns):
- asyncThrow
I'll be submitting a patch shortly that will fix this issue.
Turns out github created a new issue when i submitted my PR, it's under #245
With the patch, functions thrown asynchronously now print their stacks like so:
└─[0] <git:(master✱✈)> ./bin/nodeunit ./test/foo.test.js
foo.test.js
Error: thrown asynchronously
at Object._onImmediate (/Users/yunong/workspace/nodeunit/test/foo.test.js:2:37)
at processImmediate [as _immediateCallback] (timers.js:330:15)
FAILURES: Undone tests (or their setups/teardowns):
- asyncThrow
To fix this, make sure all tests call test.done()
:+1:
This looks like a great improvement... why hasn't anything happened with this anymore?
:+1:
@caolan Can we merge this patch?