Skip to content

Commit

Permalink
Merge pull request #74 from alecxe/master
Browse files Browse the repository at this point in the history
feat(rules): add 'valid-suite-description' rule
  • Loading branch information
lo1tuma authored Jul 4, 2016
2 parents 86e734e + b39dfb8 commit d44623e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
* [no-synchronous-tests](no-synchronous-tests.md) - disallow synchronous tests
* [no-global-tests](no-global-tests.md) - disallow global tests
* [valid-test-description](valid-test-description.md) - match test descriptions against a pre-configured regular expression
* [valid-suite-description](valid-suite-description.md) - match suite descriptions against a pre-configured regular expression
51 changes: 51 additions & 0 deletions docs/rules/valid-suite-description.md
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"]]
},
```
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
'handle-done-callback': require('./lib/rules/handle-done-callback'),
'no-synchronous-tests': require('./lib/rules/no-synchronous-tests'),
'no-global-tests': require('./lib/rules/no-global-tests'),
'valid-test-description': require('./lib/rules/valid-test-description')
'valid-test-description': require('./lib/rules/valid-test-description'),
'valid-suite-description': require('./lib/rules/valid-suite-description')
},
configs: {
recommended: {
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/valid-suite-description.js
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.');
}
}
}
};
};
62 changes: 62 additions & 0 deletions test/rules/valid-suite-description.js
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.' }
]
}
]
});

0 comments on commit d44623e

Please sign in to comment.