Skip to content

Commit

Permalink
Handle errors thrown outside test blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrhodes committed Jun 24, 2016
1 parent 83f5d09 commit 7fc935a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
21 changes: 20 additions & 1 deletion index.js
@@ -1,6 +1,7 @@
var tape = require('tape')
var global = require('global')
var tests = require('./manager')()
var harness

exports = module.exports = tape

Expand Down Expand Up @@ -32,10 +33,28 @@ exports.Test.prototype.run = function () {
this.emit('run')
}


var createHarness = exports.createHarness
exports.createHarness = function () {
harness = createHarness.apply(this, arguments)
return harness
}

process.browser ?
global.onerror = killall :
process.on('uncaughtException', killall)

function killall (err) {
tests.killall(err)
if (tests.killall(err)) return

// Died outside of a test block
harness ?
harness('uncaughtException', die) :
tape('uncaughtException', die)

function die (test) {
test.error(err)
test.end()
}
}

4 changes: 4 additions & 0 deletions manager.js
Expand Up @@ -22,11 +22,15 @@ Manager.prototype.remove = function (test) {
Manager.prototype.killall = function (err) {
var test
var tests = this.tests.slice()
if (!tests.length) return false

var single = tests.length === 1
while (test = tests.shift()) {
if (single) test.error(err)
test._end()
}

return true
}

module.exports = Manager
6 changes: 3 additions & 3 deletions test/catch-exceptions.js
Expand Up @@ -16,15 +16,15 @@ test('catch exceptions', function (t) {
});
ps.stdout.pipe(concat(function (body) {
var results = body.toString();
if (/ReferenceError/.test(results)) {
if (/(Reference|Syntax)Error/.test(results)) {
t.pass('reported exception');
}
var tests = (/# tests\s+([0-9]+)/.exec(results) || [])[1];
var pass = (/# pass\s+([0-9]+)/.exec(results) || [])[1];
var fail = (/# fail\s+([0-9]+)/.exec(results) || [])[1];
t.equal(tests, '4');
t.equal(tests, '5');
t.equal(pass, '1');
t.equal(fail, '3');
t.equal(fail, '4');
}));
});
});
Expand Down
2 changes: 2 additions & 0 deletions test/catch-exceptions/catch-harness.js
Expand Up @@ -28,3 +28,5 @@ test('still run this test', function (assert) {
assert.equal(1 + 1, 2, 'this still ran');
assert.end();
});

eval('cause an exception outside of a tape block');
2 changes: 2 additions & 0 deletions test/catch-exceptions/catch.js
Expand Up @@ -22,3 +22,5 @@ tape('still run this test', function (assert) {
assert.equal(1 + 1, 2, 'this still ran');
assert.end();
});

eval('cause an exception outside of a tape block');

0 comments on commit 7fc935a

Please sign in to comment.