Skip to content
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

fatal errors in test callbacks are not displayed (with fix) #279

Open
andrasq opened this issue Sep 27, 2014 · 5 comments
Open

fatal errors in test callbacks are not displayed (with fix) #279

andrasq opened this issue Sep 27, 2014 · 5 comments

Comments

@andrasq
Copy link

andrasq commented Sep 27, 2014

Fatal errors in callbacks defined in the test functions are not displayed,
only the "test.done() not called" diagnostic is shown. It's useful to have
the error and line number too. (If not in a callback, the error is reported
as expected.)

To reproduce the issue, create a file t.js:

module.exports = {
    test: function(t) {
        var x;
        setTimeout(function() {
            x.a = 1;
        }, 1);
    }
}

Current nodeunit outputs:

% nodeunit t.js

t.js

FAILURES: Undone tests (or their setups/teardowns): 
- test

To fix this, make sure all tests call test.done()

with the propsed fix it outputs:

% nodeunit t.js

t.js
Uncaught exception:
TypeError: Cannot set property 'a' of undefined
    at null._onTimeout (/home/andras/node/t.js:5:21)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

FAILURES: Undone tests (or their setups/teardowns): 
- test

To fix this, make sure all tests call test.done()

The proposed fix:

diff --git a/lib/track.js b/lib/track.js
index 5af98ad..f699fce 100644
--- a/lib/track.js
+++ b/lib/track.js
@@ -27,6 +27,11 @@ exports.createTracker = function (on_exit) {
         }
     };

+    // AR: display errors in callbacks defined within the tests
+    process.on('uncaughtException', function(e) {
+        console.log("Uncaught exception:\n" + e.stack);
+    });
+
     process.on('exit', function() {
         on_exit = on_exit || exports.default_on_exit;
         on_exit(tracker);
@rpaterson
Copy link

I want this too, but see issue #177 :-(

@rpaterson
Copy link

With my PR #281 the output of your test t.js above is:

rpaterson@baloo2:nodeunit (domains)$ dist/nodeunit/bin/nodeunit t.js 

t.js
✖ test

TypeError: Cannot set property 'a' of undefined
  at [object Object]._onTimeout (/home/rpaterson/Programming/signpost/nodeunit/t.js:5:17)
  at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)



FAILURES: 1/1 assertions failed (9ms)

@andrasq
Copy link
Author

andrasq commented Sep 29, 2014

@rpaterson, your output is cleaner, works for me! If not yours, my "fix" is passive and doesn't change the active code.

@eladb
Copy link

eladb commented Feb 24, 2015

👍

@nategood
Copy link

Huge +1 here. Can we get this PR in? #281

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants