From da238fa294a7958d09c752182f7a7822a4349e8b 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 | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 374a6143bf..5f148d8abd 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 @@ -386,7 +385,7 @@ Runner.prototype.hook = function(name, fn) { if (testError) { self.fail(self.test, testError); } - // conditional this.skip() + // conditional skip if (hook.pending) { if (name === HOOK_TYPE_AFTER_ALL) { utils.deprecate( @@ -394,11 +393,20 @@ 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) { + // TODO define and implement use case 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 { + // TODO throw error for afterAll suite.tests.forEach(function(test) { test.pending = true; }); @@ -619,6 +627,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 +643,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 +653,13 @@ Runner.prototype.runTests = function(suite, fn) { self.emit(constants.EVENT_TEST_PENDING, test); } self.emit(constants.EVENT_TEST_END, test); - return next(); + // skip inner 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 +667,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;