Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

feat!: change the default of --forbid-only to check for process.env.CI (#5496)

Robin Van den Broeck3d94ddea2f45
diff --git a/docs/index.md b/docs/index.md
index 955327aa..0e0f8c4e 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -947,6 +947,12 @@ Enforce a rule that tests may not be exclusive (use of e.g., `describe.only()` o
`--forbid-only` causes Mocha to fail when an exclusive ("only'd") test or suite is encountered, and it will abort further test execution.
+Defaults:
+
+1. Before v12: false
+2. After v12: false, unless an environment variable called `CI` is set. Many popular
+ CI providers, like github actions or gitlab, do this automatically.
+
### `--forbid-pending`
Enforce a rule that tests may not be skipped (use of e.g., `describe.skip()`, `it.skip()`, or `this.skip()` anywhere is disallowed).
diff --git a/lib/cli/run.js b/lib/cli/run.js
index 384528fd..631170c0 100644
--- a/lib/cli/run.js
+++ b/lib/cli/run.js
@@ -26,6 +26,7 @@ const { ONE_AND_DONES, ONE_AND_DONE_ARGS } = require("./one-and-dones");
const debug = require("debug")("mocha:cli:run");
const defaults = require("../mocharc.json");
const { types, aliases } = require("./run-option-metadata");
+const { isCI } = require("../utils");
/**
* Logical option groups
@@ -124,6 +125,7 @@ exports.builder = (yargs) =>
"forbid-only": {
description: "Fail if exclusive test(s) encountered",
group: GROUPS.RULES,
+ default: isCI(),
},
"forbid-pending": {
description: "Fail if pending test(s) encountered",
diff --git a/lib/errors.js b/lib/errors.js
index cc2d5b8a..ae085d9b 100644
--- a/lib/errors.js
+++ b/lib/errors.js
@@ -9,6 +9,7 @@
const { format } = require("node:util");
const { constants } = require("./error-constants.js");
+const { isCI } = require("./utils");
/**
* Contains error codes, factory functions to create throwable error objects,
@@ -329,11 +330,17 @@ function createMultipleDoneError(runnable, originalErr) {
* @returns {Error} Error with code {@link constants.FORBIDDEN_EXCLUSIVITY}
*/
function createForbiddenExclusivityError(mocha) {
- var err = new Error(
- mocha.isWorker
- ? "`.only` is not supported in parallel mode"
- : "`.only` forbidden by --forbid-only",
- );
+ var message;
+ if (mocha.isWorker) {
+ message = "`.only` is not supported in parallel mode";
+ } else {
+ message = "`.only` forbidden by --forbid-only";
+ if (isCI()) {
+ message += " (default in CI, add `--no-forbid-only` to allow `.only`)";
+ }
+ }
+
+ var err = new Error(message);
err.code = constants.FORBIDDEN_EXCLUSIVITY;
return err;
}
diff --git a/lib/utils.js b/lib/utils.js
index ecfefd92..85ae591c 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -697,3 +697,17 @@ exports.breakCircularDeps = (inputObj) => {
exports.isNumeric = (input) => {
return !isNaN(parseFloat(input));
};
+
+/**
+ * Checks if being ran in a CI environment.
+ *
+ * This uses the CI env variable, which is set by most popular CI providers. Some
+ * examples include:
+ * Github: https://docs.github.com/en/actions/reference/workflows-and-actions/variables
+ * Gitlab: https://docs.gitlab.com/ci/variables/predefined_variables/
+ * CircleCI: https://circleci.com/docs/reference/variables/#built-in-environment-variables
+ * Bitbucket: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/
+ */
+exports.isCI = () => {
+ return !!process.env.CI;
+};
diff --git a/package.json b/package.json
index 2ca471c0..d4fefd2f 100644
--- a/package.json
+++ b/package.json
@@ -71,7 +71,7 @@
"test-browser": "run-s clean build test-browser:*",
"test-coverage-clean": "rimraf .nyc_output coverage",
"test-coverage-generate": "nyc report --reporter=lcov --reporter=text",
- "test-node-run-only": "nyc --no-clean --reporter=json node bin/mocha.js",
+ "test-node-run-only": "nyc --no-clean --reporter=json node bin/mocha.js --no-forbid-only",
"test-node-run": "nyc --no-clean --reporter=json node bin/mocha.js --forbid-only",
"test-node:integration": "run-s clean build && npm run -s test-node-run -- --parallel --timeout 10000 --slow 3750 \"test/integration/**/*.spec.js\"",
"test-node:integration:watch": "run-s clean build && npm run -s test-node-run -- --parallel --timeout 10000 --slow 3750 \"test/integration/options/watch.spec.js\"",
diff --git a/test/integration/common-js-require.spec.js b/test/integration/common-js-require.spec.js
index 5aceb830..f006f42d 100644
--- a/test/integration/common-js-require.spec.js
+++ b/test/integration/common-js-require.spec.js
@@ -6,6 +6,7 @@ describe("common js require", () => {
it("should be able to run a test where all mocha exports are used", async () => {
const result = await runMochaAsync("common-js-require.fixture.js", [
"--delay",
+ "--no-forbid-only",
]);
expect(result.output, "to contain", "running before");
expect(result.output, "to contain", "running suiteSetup");
diff --git a/test/integration/only.spec.js b/test/integration/only.spec.js
index db0b53ac..a2e8fccf 100644
--- a/test/integration/only.spec.js
+++ b/test/integration/only.spec.js
@@ -6,33 +6,41 @@ var assert = require("node:assert");
describe(".only()", function () {
describe("bdd", function () {
it("should run only tests that marked as `only`", function (done) {
- run("options/only/bdd.fixture.js", ["--ui", "bdd"], function (err, res) {
- if (err) {
- done(err);
- return;
- }
- assert.strictEqual(res.stats.pending, 0);
- assert.strictEqual(res.stats.passes, 11);
- assert.strictEqual(res.stats.failures, 0);
- assert.strictEqual(res.code, 0);
- done();
- });
+ run(
+ "options/only/bdd.fixture.js",
+ ["--ui", "bdd", "--no-forbid-only"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ assert.strictEqual(res.stats.pending, 0);
+ assert.strictEqual(res.stats.passes, 11);
+ assert.strictEqual(res.stats.failures, 0);
+ assert.strictEqual(res.code, 0);
+ done();
+ },
+ );
});
});
describe("tdd", function () {
it("should run only tests that marked as `only`", function (done) {
- run("options/only/tdd.fixture.js", ["--ui", "tdd"], function (err, res) {
- if (err) {
- done(err);
- return;
- }
- assert.strictEqual(res.stats.pending, 0);
- assert.strictEqual(res.stats.passes, 8);
- assert.strictEqual(res.stats.failures, 0);
- assert.strictEqual(res.code, 0);
- done();
- });
+ run(
+ "options/only/tdd.fixture.js",
+ ["--ui", "tdd", "--no-forbid-only"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ assert.strictEqual(res.stats.pending, 0);
+ assert.strictEqual(res.stats.passes, 8);
+ assert.strictEqual(res.stats.failures, 0);
+ assert.strictEqual(res.code, 0);
+ done();
+ },
+ );
});
});
@@ -40,7 +48,7 @@ describe(".only()", function () {
it("should run only tests that marked as `only`", function (done) {
run(
"options/only/qunit.fixture.js",
- ["--ui", "qunit"],
+ ["--ui", "qunit", "--no-forbid-only"],
function (err, res) {
if (err) {
done(err);
diff --git a/test/integration/options/delay.spec.js b/test/integration/options/delay.spec.js
index 48e8e2d6..c4b0ac53 100644
--- a/test/integration/options/delay.spec.js
+++ b/test/integration/options/delay.spec.js
@@ -5,11 +5,7 @@ var helpers = require("../helpers");
var runMochaJSON = helpers.runMochaJSON;
describe("--delay", function () {
- var args = [];
-
- before(function () {
- args = ["--delay"];
- });
+ var args = ["--delay", "--no-forbid-only"];
it("should run the generated test suite", function (done) {
var fixture = path.join("options", "delay");
diff --git a/test/integration/options/dryRun.spec.js b/test/integration/options/dryRun.spec.js
index e1eebc05..41563b11 100644
--- a/test/integration/options/dryRun.spec.js
+++ b/test/integration/options/dryRun.spec.js
@@ -5,7 +5,7 @@ var helpers = require("../helpers");
var runMochaJSON = helpers.runMochaJSON;
describe("--dry-run", function () {
- var args = ["--dry-run"];
+ var args = ["--dry-run", "--no-forbid-only"];
it("should only report, but not execute any test", function (done) {
var fixture = path.join("options/dry-run", "dry-run");
diff --git a/test/integration/regression.spec.js b/test/integration/regression.spec.js
index dd498ca9..b88cb286 100644
--- a/test/integration/regression.spec.js
+++ b/test/integration/regression.spec.js
@@ -51,28 +51,36 @@ describe("regressions", function () {
});
it("issue-2406: should run nested describe.only suites", function (done) {
- runJSON("regression/issue-2406.fixture.js", [], function (err, res) {
- if (err) {
- done(err);
- return;
- }
- expect(res, "to have passed")
- .and("not to have pending tests")
- .and("to have passed test count", 2);
- done();
- });
+ runJSON(
+ "regression/issue-2406.fixture.js",
+ ["--no-forbid-only"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ expect(res, "to have passed")
+ .and("not to have pending tests")
+ .and("to have passed test count", 2);
+ done();
+ },
+ );
});
it("issue-2417: should not recurse infinitely with .only suites nested within each other", function (done) {
- runJSON("regression/issue-2417.fixture.js", [], function (err, res) {
- if (err) {
- done(err);
- return;
- }
- expect(res, "to have passed")
- .and("not to have pending tests")
- .and("to have passed test count", 1);
- done();
- });
+ runJSON(
+ "regression/issue-2417.fixture.js",
+ ["--no-forbid-only"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ expect(res, "to have passed")
+ .and("not to have pending tests")
+ .and("to have passed test count", 1);
+ done();
+ },
+ );
});
});