From 846e59f19458efa6c8d5ceff932a424e7875ca13 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Tue, 12 May 2020 09:05:56 +0200 Subject: [PATCH 1/7] implement markOnly method for suite class --- lib/suite.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/suite.js b/lib/suite.js index f3c8b104af..24e6dd344e 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -491,6 +491,15 @@ Suite.prototype.appendOnlySuite = function(suite) { this._onlySuites.push(suite); }; +/** + * Marks a suite to be `only`. + * + * @private + */ +Suite.prototype.markOnly = function() { + this.parent && this.parent.appendOnlySuite(this); +}; + /** * Adds a test to the list of tests marked `only`. * From 4b976f5ee51c8a5dc870c694dca053796c76922c Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Tue, 12 May 2020 09:06:21 +0200 Subject: [PATCH 2/7] add test cases to cover suites markOnly method --- test/unit/suite.spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index a5063b7f91..67f17974e9 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -680,6 +680,31 @@ describe('Suite', function() { }); }); }); + + describe('.markOnly()', function() { + var sandbox; + + beforeEach(function() { + sandbox = sinon.createSandbox(); + }); + + afterEach(function() { + sandbox.restore(); + }); + + it('should call appendOnlySuite on parent', function() { + var suite = new Suite('foo'); + var spy = sandbox.spy(); + suite.parent = { + appendOnlySuite: spy + }; + suite.markOnly(); + + expect(spy, 'to have a call exhaustively satisfying', [suite]).and( + 'was called once' + ); + }); + }); }); describe('Test', function() { From a223e2c1ffef37d92d990e03eb8acf56d3793d83 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Tue, 12 May 2020 09:27:50 +0200 Subject: [PATCH 3/7] make forbidOnly option throw exception even if suite is excluded by grep --- lib/interfaces/common.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js index 5fa87e4537..debd1da9a8 100644 --- a/lib/interfaces/common.js +++ b/lib/interfaces/common.js @@ -94,6 +94,9 @@ module.exports = function(suites, context, mocha) { * @returns {Suite} */ only: function only(opts) { + if (mocha.options.forbidOnly) { + throw createForbiddenExclusivityError(mocha); + } opts.isOnly = true; return this.create(opts); }, @@ -130,12 +133,14 @@ module.exports = function(suites, context, mocha) { if (mocha.options.forbidOnly && shouldBeTested(suite)) { throw createForbiddenExclusivityError(mocha); } - suite.parent.appendOnlySuite(suite); + suite.markOnly(); } - if (suite.pending) { - if (mocha.options.forbidPending && shouldBeTested(suite)) { - throw createUnsupportedError('Pending test forbidden'); - } + if ( + suite.pending && + mocha.options.forbidPending && + shouldBeTested(suite) + ) { + throw createUnsupportedError('Pending test forbidden'); } if (typeof opts.fn === 'function') { opts.fn.call(suite); From 566dae6470c7bdb454db81c480c32248619afb5e Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Tue, 12 May 2020 09:28:34 +0200 Subject: [PATCH 4/7] adapt test cases to changed forbidOnly logic --- test/integration/options/forbidOnly.spec.js | 35 ++++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/test/integration/options/forbidOnly.spec.js b/test/integration/options/forbidOnly.spec.js index 1886becb7d..252c5b99b9 100644 --- a/test/integration/options/forbidOnly.spec.js +++ b/test/integration/options/forbidOnly.spec.js @@ -92,32 +92,37 @@ describe('--forbid-only', function() { ); }); - it('should succeed if suite marked only does not match grep', function(done) { + it('should fail if suite marked only does not match grep', function(done) { var fixture = path.join('options', 'forbid-only', 'only-suite'); - runMochaJSON(fixture, args.concat('--fgrep', 'bumble bees'), function( - err, - res - ) { - if (err) { - return done(err); - } - expect(res, 'to have passed'); - done(); - }); + var spawnOpts = {stdio: 'pipe'}; + runMocha( + fixture, + args.concat('--fgrep', 'bumble bees'), + function(err, res) { + if (err) { + return done(err); + } + expect(res, 'to have failed with output', new RegExp(onlyErrorMessage)); + done(); + }, + spawnOpts + ); }); - it('should succeed if suite marked only does not match inverted grep', function(done) { + it('should fail if suite marked only does not match inverted grep', function(done) { var fixture = path.join('options', 'forbid-only', 'only-suite'); - runMochaJSON( + var spawnOpts = {stdio: 'pipe'}; + runMocha( fixture, args.concat('--fgrep', 'suite marked with only', '--invert'), function(err, res) { if (err) { return done(err); } - expect(res, 'to have passed'); + expect(res, 'to have failed with output', new RegExp(onlyErrorMessage)); done(); - } + }, + spawnOpts ); }); From 5c58d0a15ca721646d157f45b878d73e3b7cb247 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Tue, 2 Jun 2020 10:53:28 +0200 Subject: [PATCH 5/7] reuse existing sandbox variable --- test/unit/suite.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index 67f17974e9..0629a94e79 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -682,8 +682,6 @@ describe('Suite', function() { }); describe('.markOnly()', function() { - var sandbox; - beforeEach(function() { sandbox = sinon.createSandbox(); }); From af6281959f3b5e2d2f14582b9b892ced5a1b9fde Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Wed, 3 Jun 2020 10:19:37 +0200 Subject: [PATCH 6/7] remove unnecessary check for forbid only option --- lib/interfaces/common.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js index debd1da9a8..3c171ef8d7 100644 --- a/lib/interfaces/common.js +++ b/lib/interfaces/common.js @@ -130,9 +130,6 @@ module.exports = function(suites, context, mocha) { suite.file = opts.file; suites.unshift(suite); if (opts.isOnly) { - if (mocha.options.forbidOnly && shouldBeTested(suite)) { - throw createForbiddenExclusivityError(mocha); - } suite.markOnly(); } if ( From cc7277d0e45c5937bd4b8cd29de8e0a43e4d660c Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Fri, 5 Jun 2020 10:08:00 +0200 Subject: [PATCH 7/7] remove duplicate beforeEach afterEach defined for markOnly test suite --- test/unit/suite.spec.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index 0629a94e79..6166496ee5 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -682,14 +682,6 @@ describe('Suite', function() { }); describe('.markOnly()', function() { - beforeEach(function() { - sandbox = sinon.createSandbox(); - }); - - afterEach(function() { - sandbox.restore(); - }); - it('should call appendOnlySuite on parent', function() { var suite = new Suite('foo'); var spy = sandbox.spy();