☕️ Classic, reliable, trusted test framework for Node.js and the browser
--forbid-only doesn't recognize `it.only` when `before` crashes (#4256); closes #3840
Arvid Ottenberg184036f00878
diff --git a/lib/interfaces/common.js b/lib/interfaces/common.jsindex 7991e113..e7e84b25 100644--- a/lib/interfaces/common.js+++ b/lib/interfaces/common.js@@ -3,6 +3,7 @@var Suite = require('../suite');var errors = require('../errors');var createMissingArgumentError = errors.createMissingArgumentError;+var createUnsupportedError = errors.createUnsupportedError;/*** Functions common to more than one interface.@@ -126,14 +127,14 @@ module.exports = function(suites, context, mocha) {suites.unshift(suite);if (opts.isOnly) {if (mocha.options.forbidOnly && shouldBeTested(suite)) {- throw new Error('`.only` forbidden');+ throw createUnsupportedError('`.only` forbidden');}suite.parent.appendOnlySuite(suite);}if (suite.pending) {if (mocha.options.forbidPending && shouldBeTested(suite)) {- throw new Error('Pending test forbidden');+ throw createUnsupportedError('Pending test forbidden');}}if (typeof opts.fn === 'function') {@@ -165,6 +166,8 @@ module.exports = function(suites, context, mocha) {* @returns {*}*/only: function(mocha, test) {+ if (mocha.options.forbidOnly)+ throw createUnsupportedError('`.only` forbidden');test.markOnly();return test;},diff --git a/lib/runner.js b/lib/runner.jsindex d87c4182..2f38fa8e 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -581,11 +581,6 @@ Runner.prototype.runTest = function(fn) {return;}- var suite = this.parents().reverse()[0] || this.suite;- if (this.forbidOnly && suite.hasOnly()) {- fn(new Error('`.only` forbidden'));- return;- }if (this.asyncOnly) {test.asyncOnly = true;}diff --git a/test/integration/fixtures/options/forbid-only/only-before-each.fixture.js b/test/integration/fixtures/options/forbid-only/only-before-each.fixture.jsnew file mode 100644index 00000000..19bfb864--- /dev/null+++ b/test/integration/fixtures/options/forbid-only/only-before-each.fixture.js@@ -0,0 +1,8 @@+'use strict';++describe('test marked with only and beforeEach has skip', function() {+ beforeEach(function() {+ this.skip();+ });+ it.only('only test', function() {});+});diff --git a/test/integration/fixtures/options/forbid-only/only-before.fixture.js b/test/integration/fixtures/options/forbid-only/only-before.fixture.jsnew file mode 100644index 00000000..3924ac40--- /dev/null+++ b/test/integration/fixtures/options/forbid-only/only-before.fixture.js@@ -0,0 +1,8 @@+'use strict';++describe('test marked with only and before has skip', function() {+ before(function() {+ this.skip();+ });+ it.only('only test', function() {});+});diff --git a/test/integration/options/forbidOnly.spec.js b/test/integration/options/forbidOnly.spec.jsindex 592f0f25..1886becb 100644--- a/test/integration/options/forbidOnly.spec.js+++ b/test/integration/options/forbidOnly.spec.js@@ -26,13 +26,19 @@ describe('--forbid-only', function() {it('should fail if there are tests marked only', function(done) {var fixture = path.join('options', 'forbid-only', 'only');- runMochaJSON(fixture, args, function(err, res) {- if (err) {- return done(err);- }- expect(res, 'to have failed with error', onlyErrorMessage);- done();- });+ var spawnOpts = {stdio: 'pipe'};+ runMocha(+ fixture,+ args,+ function(err, res) {+ if (err) {+ return done(err);+ }+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));+ done();+ },+ spawnOpts+ );});it('should fail if there are tests in suites marked only', function(done) {@@ -45,11 +51,7 @@ describe('--forbid-only', function() {if (err) {return done(err);}-- expect(res, 'to satisfy', {- code: 1,- output: new RegExp(onlyErrorMessage)- });+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));done();},spawnOpts@@ -66,10 +68,7 @@ describe('--forbid-only', function() {if (err) {return done(err);}- expect(res, 'to satisfy', {- code: 1,- output: new RegExp(onlyErrorMessage)- });+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));done();},spawnOpts@@ -86,10 +85,7 @@ describe('--forbid-only', function() {if (err) {return done(err);}- expect(res, 'to satisfy', {- code: 1,- output: new RegExp(onlyErrorMessage)- });+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));done();},spawnOpts@@ -124,4 +120,38 @@ describe('--forbid-only', function() {});});++ it('should fail even if before has "skip"', function(done) {+ var fixture = path.join('options', 'forbid-only', 'only-before');+ var spawnOpts = {stdio: 'pipe'};+ runMocha(+ fixture,+ args,+ function(err, res) {+ if (err) {+ return done(err);+ }+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));+ done();+ },+ spawnOpts+ );+ });++ it('should fail even if beforeEach has "skip"', function(done) {+ var fixture = path.join('options', 'forbid-only', 'only-before-each');+ var spawnOpts = {stdio: 'pipe'};+ runMocha(+ fixture,+ args,+ function(err, res) {+ if (err) {+ return done(err);+ }+ expect(res, 'to have failed with output', new RegExp(onlyErrorMessage));+ done();+ },+ spawnOpts+ );+ });});