Skip to content

Commit

Permalink
Consistent handling of --forbid-only for suites and tests (#4282)
Browse files Browse the repository at this point in the history
* implement markOnly method for suite class

* add test cases to cover suites markOnly method

* make forbidOnly option throw exception even if suite is excluded by grep

* adapt test cases to changed forbidOnly logic

* reuse existing sandbox variable

* remove unnecessary check for forbid only option

* remove duplicate beforeEach afterEach defined for markOnly test suite
  • Loading branch information
arvidOtt authored Jun 5, 2020
1 parent 0a66259 commit ca1cfc4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
18 changes: 10 additions & 8 deletions lib/interfaces/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down Expand Up @@ -127,15 +130,14 @@ 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.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);
Expand Down
9 changes: 9 additions & 0 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down
35 changes: 20 additions & 15 deletions test/integration/options/forbidOnly.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

Expand Down
15 changes: 15 additions & 0 deletions test/unit/suite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,21 @@ describe('Suite', function() {
});
});
});

describe('.markOnly()', function() {
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() {
Expand Down

0 comments on commit ca1cfc4

Please sign in to comment.