From da6af9b028476fac0646af7c8fd29e8a3c3b279f Mon Sep 17 00:00:00 2001 From: juergba Date: Mon, 6 Apr 2020 08:21:58 +0200 Subject: [PATCH] fix beforeAll pending hook pattern --- lib/runnable.js | 12 ++++++++++-- lib/runner.js | 36 +++++++++++++++--------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/runnable.js b/lib/runnable.js index 7d3011dc86..cc457cb1dc 100644 --- a/lib/runnable.js +++ b/lib/runnable.js @@ -360,7 +360,11 @@ Runnable.prototype.run = function(fn) { }; try { - callFnAsync(this.fn); + if (this.isPending()) { + // done(); + } else { + callFnAsync(this.fn); + } } catch (err) { // handles async runnables which actually run synchronously emitted = true; @@ -481,7 +485,11 @@ var constants = utils.defineConstants( /** * Value of `state` prop when a `Runnable` has passed */ - STATE_PASSED: 'passed' + STATE_PASSED: 'passed', + /** + * Value of `state` prop when a `Runnable` has been skipped by user + */ + STATE_PENDING: 'pending' } ); diff --git a/lib/runner.js b/lib/runner.js index 8e7c8736c0..dcbc1181f9 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -18,6 +18,7 @@ var HOOK_TYPE_BEFORE_ALL = Suite.constants.HOOK_TYPE_BEFORE_ALL; var EVENT_ROOT_SUITE_RUN = Suite.constants.EVENT_ROOT_SUITE_RUN; var STATE_FAILED = Runnable.constants.STATE_FAILED; var STATE_PASSED = Runnable.constants.STATE_PASSED; +var STATE_PENDING = Runnable.constants.STATE_PENDING; var dQuote = utils.dQuote; var ngettext = utils.ngettext; var sQuote = utils.sQuote; @@ -122,8 +123,7 @@ module.exports = Runner; * @public * @class * @param {Suite} suite Root suite - * @param {boolean} [delay] Whether or not to delay execution of root suite - * until ready. + * @param {boolean} [delay] Whether to delay execution of root suite until ready. */ function Runner(suite, delay) { var self = this; @@ -285,11 +285,13 @@ Runner.prototype.checkGlobals = function(test) { * Fail the given `test`. * * @private - * @param {Test} test + * @param {Runnable} test * @param {Error} err + * @param {boolean} [force=false] - Whether to fail a pending test. */ -Runner.prototype.fail = function(test, err) { - if (test.isPending()) { +Runner.prototype.fail = function(test, err, force) { + force = force === true; + if (test.isPending() && !force) { return; } @@ -412,6 +414,7 @@ Runner.prototype.hook = function(name, fn) { suite.suites.forEach(function(suite) { suite.pending = true; }); + hooks = []; } else { hook.pending = false; var errForbid = createUnsupportedError('`this.skip` forbidden'); @@ -634,10 +637,9 @@ Runner.prototype.runTests = function(suite, fn) { // static skip, no hooks are executed if (test.isPending()) { if (self.forbidPending) { - test.isPending = alwaysFalse; - self.fail(test, new Error('Pending test forbidden')); - delete test.isPending; + self.fail(test, new Error('Pending test forbidden'), true); } else { + test.state = STATE_PENDING; self.emit(constants.EVENT_TEST_PENDING, test); } self.emit(constants.EVENT_TEST_END, test); @@ -650,10 +652,9 @@ Runner.prototype.runTests = function(suite, fn) { // conditional skip within beforeEach if (test.isPending()) { if (self.forbidPending) { - test.isPending = alwaysFalse; - self.fail(test, new Error('Pending test forbidden')); - delete test.isPending; + self.fail(test, new Error('Pending test forbidden'), true); } else { + test.state = STATE_PENDING; self.emit(constants.EVENT_TEST_PENDING, test); } self.emit(constants.EVENT_TEST_END, test); @@ -674,10 +675,9 @@ Runner.prototype.runTests = function(suite, fn) { // conditional skip within it if (test.pending) { if (self.forbidPending) { - test.isPending = alwaysFalse; - self.fail(test, new Error('Pending test forbidden')); - delete test.isPending; + self.fail(test, new Error('Pending test forbidden'), true); } else { + test.state = STATE_PENDING; self.emit(constants.EVENT_TEST_PENDING, test); } self.emit(constants.EVENT_TEST_END, test); @@ -714,10 +714,6 @@ Runner.prototype.runTests = function(suite, fn) { next(); }; -function alwaysFalse() { - return false; -} - /** * Run the given `suite` and invoke the callback `fn()` when complete. * @@ -850,9 +846,7 @@ Runner.prototype.uncaught = function(err) { return; } else if (runnable.isPending()) { // report 'pending test' retrospectively as failed - runnable.isPending = alwaysFalse; - this.fail(runnable, err); - delete runnable.isPending; + this.fail(runnable, err, true); return; }