From 58cbff4d24327af8531379de0094388b7f46e1aa Mon Sep 17 00:00:00 2001 From: juergba Date: Mon, 18 Feb 2019 15:22:33 +0100 Subject: [PATCH] fix this.skip() in beforeEach hooks --- lib/runner.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 374a6143bf..77838847b2 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -315,8 +315,7 @@ Runner.prototype.fail = function(test, err) { * - Failed `before each` hook skips remaining tests in a * suite and jumps to corresponding `after each` hook, * which is run only once - * - Failed `after` hook does not alter - * execution order + * - Failed `after` hook does not alter execution order * - Failed `after each` hook skips remaining tests in a * suite and subsuites, but executes other `after each` * hooks @@ -394,10 +393,17 @@ Runner.prototype.hook = function(name, fn) { 'Use a return statement or other means to abort hook execution.' ); } - if (name === HOOK_TYPE_BEFORE_EACH || name === HOOK_TYPE_AFTER_EACH) { + if (name === HOOK_TYPE_AFTER_EACH) { if (self.test) { self.test.pending = true; } + } else if (name === HOOK_TYPE_BEFORE_EACH) { + if (self.test) { + self.test.pending = true; + } + self.emit(constants.EVENT_HOOK_END, hook); + hook.pending = false; // activates hook for next test + return fn(new Error('abort hookDown')); } else { suite.tests.forEach(function(test) { test.pending = true; @@ -619,6 +625,7 @@ Runner.prototype.runTests = function(suite, fn) { return; } + // static skip, no hooks are executed if (test.isPending()) { if (self.forbidPending) { test.isPending = alwaysFalse; @@ -634,6 +641,7 @@ Runner.prototype.runTests = function(suite, fn) { // execute test and hook(s) self.emit(constants.EVENT_TEST_BEGIN, (self.test = test)); self.hookDown(HOOK_TYPE_BEFORE_EACH, function(err, errSuite) { + // conditional skip within beforeEach if (test.isPending()) { if (self.forbidPending) { test.isPending = alwaysFalse; @@ -643,7 +651,13 @@ Runner.prototype.runTests = function(suite, fn) { self.emit(constants.EVENT_TEST_PENDING, test); } self.emit(constants.EVENT_TEST_END, test); - return next(); + // skip afterEach hooks below errSuite level + var origSuite = self.suite; + self.suite = errSuite; + return self.hookUp(HOOK_TYPE_AFTER_EACH, function(e, eSuite) { + self.suite = origSuite; + next(e, eSuite); + }); } if (err) { return hookErr(err, errSuite, false); @@ -651,7 +665,7 @@ Runner.prototype.runTests = function(suite, fn) { self.currentRunnable = self.test; self.runTest(function(err) { test = self.test; - // conditional this.skip() + // conditional skip within it if (test.pending) { if (self.forbidPending) { test.isPending = alwaysFalse;