-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from alecxe/master
feat(rules): add 'valid-suite-description' rule
- Loading branch information
Showing
5 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Match suite descriptions against a pre-configured regular expression | ||
|
||
This rule enforces the suite descriptions to follow the desired format. | ||
|
||
## Rule Details | ||
|
||
By default, the regular expression is not configured and would be required if rule is enabled. | ||
By default, the rule supports "describe", "context" and "suite" suite function names, but it can be configured to look for different suite names via rule configuration. | ||
|
||
Example of a custom rule configuration: | ||
|
||
```js | ||
rules: { | ||
"mocha/valid-suite-description": ["warning", "^[A-Z]"] | ||
}, | ||
``` | ||
|
||
where: | ||
|
||
* `warning` is a rule error level (see [Configuring Rules](http://eslint.org/docs/user-guide/configuring#configuring-rules)) | ||
* `^[A-Z]` is a custom regular expression pattern to match suite names against; `^[A-Z]` enforces a suite name to start with an upper-case letter | ||
|
||
The following patterns are considered warnings (with the example rule configuration posted above): | ||
|
||
```js | ||
// bdd | ||
describe("something to test", function() { }); | ||
context("something to test", function() { }); | ||
|
||
// tdd | ||
suite("something to test", function() { }); | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
// bdd | ||
describe("Test suite", function() { }); | ||
context("Test suite", function() { }); | ||
|
||
// tdd | ||
suite("Test suite", function() { }); | ||
``` | ||
|
||
There is also possible to configure a custom list of suite names via the second rule configuration option: | ||
|
||
```js | ||
rules: { | ||
"mocha/valid-suite-description": ["warning", "^[A-Z]", ["describe", "context", "suite", "mysuitename"]] | ||
}, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @fileoverview Match suite descriptions to match a pre-configured regular expression | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var defaultSuiteNames = [ 'describe', 'context', 'suite' ]; | ||
|
||
module.exports = function (context) { | ||
var pattern = new RegExp(context.options[0]), | ||
suiteNames = context.options[1] ? context.options[1] : defaultSuiteNames; | ||
|
||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee, | ||
args; | ||
|
||
if (callee && callee.name && suiteNames.indexOf(callee.name) > -1) { | ||
args = node.arguments; | ||
if (args && args[0] && typeof args[0].value === 'string' && !pattern.test(args[0].value)) { | ||
context.report(node, 'Invalid "' + callee.name + '()" description found.'); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
var RuleTester = require('eslint').RuleTester, | ||
rules = require('../../').rules, | ||
ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('valid-suite-description', rules['valid-suite-description'], { | ||
valid: [ | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'describe("This is a test", function () { });' | ||
}, | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'context("This is a test", function () { });' | ||
}, | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'suite("This is a test", function () { });' | ||
}, | ||
{ | ||
options: [ '^[A-Z]', [ 'someFunction' ] ], | ||
code: 'describe("this is a test", function () { });' | ||
}, | ||
{ | ||
options: [ '^[A-Z]', [ 'someFunction' ] ], | ||
code: 'someFunction("Should do something", function () { });' | ||
}, | ||
'someOtherFunction();' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'describe("this is a test", function () { });', | ||
errors: [ | ||
{ message: 'Invalid "describe()" description found.' } | ||
] | ||
}, | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'context("this is a test", function () { });', | ||
errors: [ | ||
{ message: 'Invalid "context()" description found.' } | ||
] | ||
}, | ||
{ | ||
options: [ '^[A-Z]' ], | ||
code: 'suite("this is a test", function () { });', | ||
errors: [ | ||
{ message: 'Invalid "suite()" description found.' } | ||
] | ||
}, | ||
{ | ||
options: [ '^[A-Z]', [ 'customFunction' ] ], | ||
code: 'customFunction("this is a test", function () { });', | ||
errors: [ | ||
{ message: 'Invalid "customFunction()" description found.' } | ||
] | ||
} | ||
] | ||
}); |