Skip to content

Commit

Permalink
Merge pull request #262 from lo1tuma/consistent-settings
Browse files Browse the repository at this point in the history
Consistently use shared settings in all rules
  • Loading branch information
lo1tuma authored Aug 5, 2020
2 parents a9c51b3 + 6fe21e6 commit 60d2a74
Show file tree
Hide file tree
Showing 37 changed files with 582 additions and 529 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,31 @@ Then add a reference to this plugin and selected rules in your eslint config:

```json
{
"plugins": [
"mocha"
],
"plugins": [
"mocha"
]
}
```

### Plugin Settings

This plugin supports the following settings, which are used by multiple rules:

* `additionalCustomNames`: This allows rules to check additional function names when looking for suites or test cases. This might be used with a custom Mocha extension, such as [`ember-mocha`](https://github.com/switchfly/ember-mocha)
**Example:**

```json
{
"rules": {
"mocha/no-skipped-tests": "error",
"mocha/no-exclusive-tests": "error"
},
"settings": {
"mocha/additionalCustomNames": [
{ "name": "describeModule", "type": "suite", "interfaces": [ "BDD" ] },
{ "name": "testModule", "type": "testCase", "interfaces": [ "TDD" ] }
}
}
}
```

Expand All @@ -34,9 +56,9 @@ Enable it with the extends option:

```json
{
"extends": [
"plugin:mocha/recommended"
],
"extends": [
"plugin:mocha/recommended"
]
}
```

Expand Down
19 changes: 0 additions & 19 deletions docs/rules/no-exclusive-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ suite.skip("bar", function () {});
test.skip("bar", function () {});
```

## Settings

This rule supports the following shared configuration setting:

* `additionalTestFunctions`: An array of extra test functions to protect. This might be used with a custom Mocha extension, such as [`ember-mocha`](https://github.com/switchfly/ember-mocha)

```json
{
"rules": {
"mocha/no-exclusive-tests": "error"
},
"settings": {
"mocha/additionalTestFunctions": [
"describeModule"
]
}
}
```

## When Not To Use It

* If you really want to execute only one test-suite or test-case because all other tests should not be executed, turn this rule off.
Expand Down
22 changes: 0 additions & 22 deletions docs/rules/no-skipped-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@ suite.only("bar", function () {});
test.only("bar", function () {});
```

## Settings

This rule supports the following shared configuration settings:

* `additionalTestFunctions`: An array of extra test functions to protect. This might be used with a custom Mocha extension, such as [`ember-mocha`](https://github.com/switchfly/ember-mocha)
* `additionalXFunctions`: An array of extra x-function to protect

```json
{
"rules": {
"mocha/no-skipped-tests": "error"
},
"settings": {
"mocha/additionalTestFunctions": [
"describeModule"
],
"mocha/additionalXFunctions": [
"xdescribeModule"
]
}
}
```
## When Not To Use It

* If you really want to commit skipped tests to your repo, turn this rule off.
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const find = require('ramda/src/find');
const astUtils = require('../util/ast');
const createAstUtils = require('../util/ast');

module.exports = {
meta: {
Expand All @@ -23,8 +23,8 @@ module.exports = {
]
},
create(context) {
const [ config = {} ] = context.options;
const { ignoreSkipped = false } = config;
const astUtils = createAstUtils(context.settings);
const [ { ignoreSkipped = false } = {} ] = context.options;
const modifiersToCheck = ignoreSkipped ? [ 'only' ] : [ 'only', 'skip' ];

function isAsyncFunction(functionExpression) {
Expand Down
7 changes: 3 additions & 4 deletions lib/rules/max-top-level-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

const isNil = require('ramda/src/isNil');
const astUtil = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');
const createAstUtils = require('../util/ast');

const defaultSuiteLimit = 1;

Expand All @@ -34,9 +33,9 @@ module.exports = {
]
},
create(context) {
const astUtils = createAstUtils(context.settings);
const topLevelDescribes = [];
const options = context.options[0] || {};
const settings = context.settings;
let suiteLimit;

if (isNil(options.limit)) {
Expand All @@ -47,7 +46,7 @@ module.exports = {

return {
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
if (astUtils.isDescribe(node)) {
const scope = context.getScope();

if (isTopLevelScope(scope)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-async-describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* @fileoverview Disallow async functions as arguments to describe
*/

const astUtils = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');
const createAstUtils = require('../util/ast');

module.exports = {
meta: {
Expand All @@ -18,6 +17,7 @@ module.exports = {
fixable: 'code'
},
create(context) {
const astUtils = createAstUtils(context.settings);
const sourceCode = context.getSourceCode();

function isFunction(node) {
Expand Down Expand Up @@ -62,7 +62,7 @@ module.exports = {
CallExpression(node) {
const name = astUtils.getNodeName(node.callee);

if (astUtils.isDescribe(node, additionalSuiteNames(context.settings))) {
if (astUtils.isDescribe(node)) {
const fnArg = node.arguments.slice(-1)[0];
if (isAsyncFunction(fnArg)) {
context.report({
Expand Down
38 changes: 6 additions & 32 deletions lib/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { getAdditionalTestFunctions } = require('../util/settings');
const astUtils = require('../util/ast');
const createAstUtils = require('../util/ast');

module.exports = {
meta: {
Expand All @@ -11,40 +10,15 @@ module.exports = {
}
},
create(context) {
let mochaTestFunctions = [
'it',
'describe',
'suite',
'test',
'context',
'specify'
];
const settings = context.settings;
const additionalTestFunctions = getAdditionalTestFunctions(settings);

mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);

function matchesMochaTestFunction(object) {
const name = astUtils.getNodeName(object);

return mochaTestFunctions.includes(name);
}

function isPropertyNamedOnly(property) {
return property && astUtils.getPropertyName(property) === 'only';
}

function isCallToMochasOnlyFunction(callee) {
return callee.type === 'MemberExpression' &&
matchesMochaTestFunction(callee.object) &&
isPropertyNamedOnly(callee.property);
}
const astUtils = createAstUtils(context.settings);

return {
CallExpression(node) {
const callee = node.callee;
const options = { modifiers: [ 'only' ], modifiersOnly: true };

if (astUtils.isDescribe(node, options) || astUtils.isTestCase(node, options)) {
const callee = node.callee;

if (callee && isCallToMochasOnlyFunction(callee)) {
context.report({
node: callee.property,
message: 'Unexpected exclusive mocha test.'
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-global-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const astUtils = require('../util/ast');
const createAstUtils = require('../util/ast');

module.exports = {
meta: {
Expand All @@ -10,6 +10,8 @@ module.exports = {
}
},
create(context) {
const astUtils = createAstUtils(context.settings);

function isGlobalScope(scope) {
return scope.type === 'global' || scope.type === 'module';
}
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/no-hooks-for-single-case.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const astUtil = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');
const createAstUtils = require('../util/ast');

function newDescribeLayer(describeNode) {
return {
Expand Down Expand Up @@ -32,9 +31,9 @@ module.exports = {
]
},
create(context) {
const astUtils = createAstUtils(context.settings);
const options = context.options[0] || {};
const allowedHooks = options.allow || [];
const settings = context.settings;
let layers = [];

function increaseTestCount() {
Expand Down Expand Up @@ -72,17 +71,17 @@ module.exports = {
},

CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
if (astUtils.isDescribe(node)) {
increaseTestCount();
layers.push(newDescribeLayer(node));
return;
}

if (astUtil.isTestCase(node)) {
if (astUtils.isTestCase(node)) {
increaseTestCount();
}

if (astUtil.isHookIdentifier(node.callee)) {
if (astUtils.isHookIdentifier(node.callee)) {
layers[layers.length - 1].hookNodes.push(node.callee);
}
},
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const astUtil = require('../util/ast');
const createAstUtils = require('../util/ast');

module.exports = {
meta: {
Expand All @@ -25,14 +25,15 @@ module.exports = {
},

create(context) {
const astUtils = createAstUtils(context.settings);
const [ config = {} ] = context.options;
const { allow = [] } = config;

return {
CallExpression(node) {
const isHookAllowed = allow.includes(node.callee.name);

if (astUtil.isHookIdentifier(node.callee) && !isHookAllowed) {
if (astUtils.isHookIdentifier(node.callee) && !isHookAllowed) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
Expand Down
Loading

0 comments on commit 60d2a74

Please sign in to comment.