nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

Exceptions in test code are missed

Open mkschreder opened this issue 11 years ago • 7 comments

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.

mkschreder avatar Jan 23 '14 20:01 mkschreder

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.

caolan avatar Jan 23 '14 20:01 caolan

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.

yunong avatar Jan 29 '14 19:01 yunong

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()

yunong avatar Jan 29 '14 19:01 yunong

:+1:

christiaan avatar Feb 19 '14 08:02 christiaan

This looks like a great improvement... why hasn't anything happened with this anymore?

arendjr avatar Dec 30 '14 18:12 arendjr

:+1:

StuartHa avatar Mar 04 '15 18:03 StuartHa

@caolan Can we merge this patch?

yunong avatar Mar 04 '15 18:03 yunong