☕️ Classic, reliable, trusted test framework for Node.js and the browser
refactored hook failures
Tj Holowaychuk1c84df3e45f7
diff --git a/lib/runner.js b/lib/runner.jsindex f748c8f7..6625b736 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -92,23 +92,26 @@ Runner.prototype.fail = function(test, err){};/**- * Fail the given `hook` name.+ * Fail the given `hook` with `err`.*- * @param {String} hook+ * Hook failures (currently) hard-exit due+ * to that fact that a failing hook will+ * surely cause subsequent tests to fail,+ * causing jumbled reporting.+ *+ * @param {Hook} hook* @param {Error} err* @api private*/Runner.prototype.failHook = function(hook, err){- var test = new Test(hook + ' hook', noop);- test.parent = this.suite;- this.fail(test, err);+ this.fail(hook, err);this.emit('end');process.exit(0);};/**- * Run hook `name` callbacks and then invoke `fn(err)`.+ * Run hook `name` callbacks and then invoke `fn()`.** @param {String} name* @param {Function} function@@ -119,14 +122,19 @@ Runner.prototype.hook = function(name, fn){var suite = this.suite, hooks = suite['_' + name], ms = suite._timeout+ , self = this, timer;function next(i) {var hook = hooks[i];if (!hook) return fn();- hook.on('error', fn);++ hook.on('error', function(err){+ self.failHook(hook, err);+ });+hook.run(function(err){- if (err) return fn(err);+ if (err) return self.failHook(hook, err);next(++i);});}@@ -272,18 +280,13 @@ Runner.prototype.runTests = function(suite, fn){// execute test and hook(s)self.emit('test', self.test = test);- self.hookDown('beforeEach', function(err){- if (err) return self.failHook('beforeEach', err);+ self.hookDown('beforeEach', function(){self.runTest(function(err){if (err) return next(err);self.emit('pass', test);test.passed = true;self.emit('test end', test);- if (err) return self.failHook('beforeEach', err);- self.hookUp('afterEach', function(err){- if (err) return self.failHook('afterEach', err);- next();- });+ self.hookUp('afterEach', next);});});}@@ -314,15 +317,13 @@ Runner.prototype.runSuite = function(suite, fn){function done() {self.suite = suite;- self.hook('afterAll', function(err){- if (err) return self.failHook('afterAll', err);+ self.hook('afterAll', function(){self.emit('suite end', suite);fn();});}- this.hook('beforeAll', function(err){- if (err) return self.failHook('beforeAll', err);+ this.hook('beforeAll', function(){self.runTests(suite, next);});};diff --git a/test.js b/test.jsindex 3d8afa0c..67c642e5 100644--- a/test.js+++ b/test.js@@ -2,8 +2,6 @@describe('something', function(){beforeEach(function(done){done()- done()- done()})it('should work', function(){