Skip to content

Commit 79e2adf

Browse files
committed
feat: improve error messages when using --forbid-only in CI
1 parent a856d6e commit 79e2adf

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/cli/run.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {ONE_AND_DONES, ONE_AND_DONE_ARGS} = require('./one-and-dones');
2626
const debug = require('debug')('mocha:cli:run');
2727
const defaults = require('../mocharc.json');
2828
const {types, aliases} = require('./run-option-metadata');
29+
const {isCI} = require('../utils');
2930

3031
/**
3132
* Logical option groups
@@ -124,7 +125,7 @@ exports.builder = yargs =>
124125
'forbid-only': {
125126
description: 'Fail if exclusive test(s) encountered',
126127
group: GROUPS.RULES,
127-
default: !!process.env.CI,
128+
default: isCI()
128129
},
129130
'forbid-pending': {
130131
description: 'Fail if pending test(s) encountered',

lib/errors.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
const {format} = require('node:util');
1111
const { constants } = require('./error-constants.js');
12+
const { isCI } = require('./utils');
1213

1314
/**
1415
* Contains error codes, factory functions to create throwable error objects,
@@ -329,11 +330,17 @@ function createMultipleDoneError(runnable, originalErr) {
329330
* @returns {Error} Error with code {@link constants.FORBIDDEN_EXCLUSIVITY}
330331
*/
331332
function createForbiddenExclusivityError(mocha) {
332-
var err = new Error(
333-
mocha.isWorker
334-
? '`.only` is not supported in parallel mode'
335-
: '`.only` forbidden by --forbid-only'
336-
);
333+
var message;
334+
if (mocha.isWorker) {
335+
message = '`.only` is not supported in parallel mode';
336+
} else {
337+
message = '`.only` forbidden by --forbid-only';
338+
if (isCI()) {
339+
message += ' (default in CI, add `--no-forbid-only` to allow `.only`)';
340+
}
341+
}
342+
343+
var err = new Error(message);
337344
err.code = constants.FORBIDDEN_EXCLUSIVITY;
338345
return err;
339346
}

lib/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,17 @@ exports.breakCircularDeps = inputObj => {
696696
exports.isNumeric = input => {
697697
return !isNaN(parseFloat(input));
698698
};
699+
700+
/**
701+
* Checks if being ran in a CI environment.
702+
*
703+
* This uses the CI env variable, which is set by most popular CI providers. Some
704+
* examples include:
705+
* Github: https://docs.github.com/en/actions/reference/workflows-and-actions/variables
706+
* Gitlab: https://docs.gitlab.com/ci/variables/predefined_variables/
707+
* CircleCI: https://circleci.com/docs/reference/variables/#built-in-environment-variables
708+
* Bitbucket: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/
709+
*/
710+
exports.isCI = () => {
711+
return !!process.env.CI;
712+
};

0 commit comments

Comments
 (0)