Skip to content

Commit

Permalink
Merge pull request #207 from BourgoisMickael/suite-custom-message
Browse files Browse the repository at this point in the history
Add custom message for valid-suite-description
  • Loading branch information
lo1tuma committed Aug 22, 2019
2 parents 91d38c3 + 61a4c67 commit b3061ba
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
12 changes: 8 additions & 4 deletions docs/rules/valid-suite-description.md
@@ -1,10 +1,10 @@
# Match suite descriptions against a pre-configured regular expression (valid-suite-description)

This rule enforces the suite descriptions to follow the desired format.
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 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:
Expand Down Expand Up @@ -42,10 +42,14 @@ context("Test suite", function() { });
suite("Test suite", function() { });
```

There is also possible to configure a custom list of suite names via the second rule configuration option:
There is also possible to configure a custom list of suite names and a custom error message via the second and third rule configuration option:

```js
rules: {
"mocha/valid-suite-description": ["warn", "^[A-Z]", ["describe", "context", "suite", "mysuitename"]]
"mocha/valid-suite-description": ["warn", "^[A-Z]", ["describe", "context", "suite", "mysuitename"], "custom error message"]
},
// OR
rules: {
"mocha/valid-suite-description": ["warn", { pattern: "^[A-Z]", suiteNames: ["describe", "context", "suite", "mysuitename"], message: "custom error message" }]
},
```
23 changes: 21 additions & 2 deletions lib/rules/valid-suite-description.js
Expand Up @@ -8,9 +8,28 @@
const astUtils = require('../util/ast');
const defaultSuiteNames = [ 'describe', 'context', 'suite' ];

module.exports = function (context) {
function inlineOptions(context) {
const pattern = new RegExp(context.options[0]);
const suiteNames = context.options[1] ? context.options[1] : defaultSuiteNames;
const message = context.options[2];

return { pattern, suiteNames, message };
}

function objectOptions(options) {
const pattern = new RegExp(options.pattern);
const suiteNames = options.suiteNames ? options.suiteNames : defaultSuiteNames;
const message = options.message;

return { pattern, suiteNames, message };
}

module.exports = function (context) {
const options = context.options[0];

const { pattern, suiteNames, message } = typeof options === 'object' && !(options instanceof RegExp) ?
objectOptions(options) :
inlineOptions(context);

function isSuite(node) {
return node.callee && node.callee.name && suiteNames.indexOf(node.callee.name) > -1;
Expand Down Expand Up @@ -40,7 +59,7 @@ module.exports = function (context) {

if (isSuite(node)) {
if (!hasValidOrNoSuiteDescription(node)) {
context.report(node, `Invalid "${ callee.name }()" description found.`);
context.report(node, message || `Invalid "${ callee.name }()" description found.`);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/rules/valid-suite-description.js
Expand Up @@ -26,6 +26,26 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
options: [ '^[A-Z]', [ 'someFunction' ] ],
code: 'someFunction("Should do something", function () { });'
},
{
options: [ '^[A-Z]', [ 'someFunction' ], 'some error message' ],
code: 'someFunction("Should do something", function () { });'
},
{
options: [ /^[A-Z]/, [ 'someFunction' ], 'some error message' ],
code: 'someFunction("Should do something", function () { });'
},
{
options: [ { pattern: '^[A-Z]', suiteNames: [ 'someFunction' ], message: 'some error message' } ],
code: 'someFunction("Should do something", function () { });'
},
{
options: [ { pattern: /^[A-Z]/, suiteNames: [ 'someFunction' ], message: 'some error message' } ],
code: 'someFunction("Should do something", function () { });'
},
{
options: [ {} ],
code: 'someFunction("Should do something", function () { });'
},
'someOtherFunction();',
{
parserOptions: { ecmaVersion: 2017 },
Expand Down Expand Up @@ -63,6 +83,20 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
errors: [
{ message: 'Invalid "customFunction()" description found.' }
]
},
{
options: [ '^[A-Z]', [ 'customFunction' ], 'some error message' ],
code: 'customFunction("this is a test", function () { });',
errors: [
{ message: 'some error message' }
]
},
{
options: [ { pattern: '^[A-Z]', suiteNames: [ 'customFunction' ], message: 'some error message' } ],
code: 'customFunction("this is a test", function () { });',
errors: [
{ message: 'some error message' }
]
}
]
});

0 comments on commit b3061ba

Please sign in to comment.