Skip to content

Commit

Permalink
Merge pull request #10 from levibuzolic/config-blocks-and-focus
Browse files Browse the repository at this point in the history
Allow users to configure their own block/focus matchers in their eslint config
  • Loading branch information
levibuzolic authored Apr 4, 2019
2 parents 13c5cbe + ad2b9d0 commit c42e1de
Show file tree
Hide file tree
Showing 7 changed files with 862 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
arrowParens: avoid
bracketSpacing: false
jsxBracketSameLine: false
parser: babel-flow
printWidth: 120
semi: true
singleQuote: true
trailingComma: es5
useTabs: false
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v2.3.0

* Allow test block names to be specified in options - #10

# v2.2.0

* Added rule for catching `.only` blocks for `serial` - #9 @IevgenRagulin
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

ESLint rule for `.only` tests in [mocha](https://mochajs.org/) and other JS testing libraries.

Currently matches the following test blocks: `describe`, `it`, `context`, `tape`, `test`, `fixture`.
Currently matches the following test blocks by default: `describe`, `it`, `context`, `tape`, `test`, `fixture`, `serial`.

Designed to prevent you from committing `.only` tests to CI, disabling tests for your whole team.

As of v2.3 you can now override the test blocks and focus functions.

## Installation

First you'll need to install [ESLint](http://eslint.org) and the plugin:
Expand Down Expand Up @@ -37,7 +39,19 @@ Then configure the rules you want to use under the rules section.
```json
{
"rules": {
"no-only-tests/no-only-tests": 2
"no-only-tests/no-only-tests": "error"
}
}
```

If you use a testing framework that uses an unsupported block name, or a different way of focusing test (something other than `.only`) you can specify an array of blocks and focus methods to match in the options.

```json
{
"rules": {
"no-only-tests/no-only-tests": ["error", {"block": ["test", "it", "assert"], "focus": ["only", "focus"]}]
}
}
```

The above example will catch any uses of `test.only`, `test.focus`, `it.only`, `it.focus`, `assert.only` and `assert.focus`.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-no-only-tests",
"version": "2.2.0",
"version": "2.3.0",
"description": "ESLint rule for .only blocks in mocha tests",
"keywords": [
"eslint",
Expand Down
58 changes: 45 additions & 13 deletions rules/no-only-tests.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
/**
* @fileoverview Rule to flag use of .only blocks in tests
* @fileoverview Rule to flag use of .only in tests, preventing focused tests being committed accidentally
* @author Levi Buzolic
*/

"use strict";
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

const BLOCK_DEFAULTS = ['describe', 'it', 'context', 'test', 'tape', 'fixture', 'serial'];
const FOCUS_DEFAULTS = ['only'];

module.exports = {
meta: {
docs: {
description: "disallow .only blocks in tests",
category: "Possible Errors",
}
description: 'disallow .only blocks in tests',
category: 'Possible Errors',
recommended: true,
url: 'https://github.com/levibuzolic/eslint-plugin-no-only-tests',
},
schema: [
{
type: 'object',
properties: {
block: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
focus: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],
},

create(context) {
const regex = /^(describe|it|context|test|tape|fixture|serial)$/;
var block = (context.options[0] || {}).block || BLOCK_DEFAULTS;
var focus = (context.options[0] || {}).focus || FOCUS_DEFAULTS;

return {
Identifier: function(node) {
if (node.name === 'only' && node.parent && node.parent.object && regex.test(node.parent.object.name)) {
context.report(node, node.parent.object.name + '.only not permitted');
Identifier(node) {
if (
focus.indexOf(node.name) != -1 &&
node.parent &&
node.parent.object &&
block.indexOf(node.parent.object.name) != -1
) {
context.report(node, node.parent.object.name + '.' + node.name + ' not permitted');
}
}
}
}
},
};
},
};
19 changes: 18 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
'xtest.only("A test block", function() {});',
'other.only("An other block", function() {});',
'var args = {only: "test"};',
'it("should pass meta only through", function() {});'
'it("should pass meta only through", function() {});',
'obscureTestBlock.only("An obscure testing library test works unless options are supplied", function() {});',
{
options: [{block: ['it']}],
code: 'test.only("Options will exclude this from being caught", function() {});',
},
{
options: [{focus: ['focus']}],
code: 'test.only("Options will exclude this from being caught", function() {});',
}
],

invalid: [{
Expand All @@ -37,6 +46,14 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
}, {
code: 'serial.only("A serial test", function() {});',
errors: [{message: 'serial.only not permitted'}]
}, {
options: [{block: ['obscureTestBlock']}],
code: 'obscureTestBlock.only("An obscure testing library test", function() {});',
errors: [{message: 'obscureTestBlock.only not permitted'}]
}, {
options: [{focus: ['focus']}],
code: 'test.focus("An alternative focus function", function() {});',
errors: [{message: 'test.focus not permitted'}]
}]
});

Expand Down
Loading

0 comments on commit c42e1de

Please sign in to comment.