Skip to content

Commit

Permalink
Merge pull request #206 from BourgoisMickael/custom-message
Browse files Browse the repository at this point in the history
Add custom message for valid-test-description rule
  • Loading branch information
lo1tuma committed Aug 22, 2019
2 parents 3102e7c + 84a4cf8 commit 91d38c3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
13 changes: 9 additions & 4 deletions docs/rules/valid-test-description.md
@@ -1,25 +1,30 @@
# Match test descriptions against a pre-configured regular expression (valid-test-description)

This rule enforces the test descriptions to follow the desired format.
This rule enforces the test descriptions to follow the desired format.

## Rule Details

By default, the regular expression is configured to be "^should" which requires test descriptions to start with "should".
By default, the regular expression is configured to be "^should" which requires test descriptions to start with "should".
By default, the rule supports "it", "specify" and "test" test function names, but it can be configured to look for different test names via rule configuration.

Example of a custom rule configuration:

```js
rules: {
"mocha/valid-test-description": ["warn", "mypattern$", ["it", "specify", "test", "mytestname"]]
"mocha/valid-test-description": ["warn", "mypattern$", ["it", "specify", "test", "mytestname"], "custom error message"]
},
// OR
rules: {
"mocha/valid-test-description": ["warn", { pattern: "mypattern$", testNames: ["it", "specify", "test", "mytestname"], message: 'custom error message' }]
},
```

where:

* `warn` is a rule error level (see [Configuring Rules](http://eslint.org/docs/user-guide/configuring#configuring-rules))
* `mypattern$` is a custom regular expression pattern to match test names against
* `["it", "specify", "test", "mytestname"]` is an array of custom test names
* `["it", "specify", "test", "mytestname"]` is an array of custom test names
* `custom error message` a custom error message to describe your pattern

The following patterns are considered warnings (with the default rule configuration):

Expand Down
23 changes: 21 additions & 2 deletions lib/rules/valid-test-description.js
Expand Up @@ -9,9 +9,28 @@ const astUtils = require('../util/ast');

const defaultTestNames = [ 'it', 'test', 'specify' ];

module.exports = function (context) {
function inlineOptions(context) {
const pattern = context.options[0] ? new RegExp(context.options[0]) : /^should/;
const testNames = context.options[1] ? context.options[1] : defaultTestNames;
const message = context.options[2];

return { pattern, testNames, message };
}

function objectOptions(options) {
const pattern = options.pattern ? new RegExp(options.pattern) : /^should/;
const testNames = options.testNames ? options.testNames : defaultTestNames;
const message = options.message;

return { pattern, testNames, message };
}

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

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

function isTest(node) {
return node.callee && node.callee.name && testNames.indexOf(node.callee.name) > -1;
Expand Down Expand Up @@ -41,7 +60,7 @@ module.exports = function (context) {

if (isTest(node)) {
if (!hasValidOrNoTestDescription(node)) {
context.report(node, `Invalid "${ callee.name }()" description found.`);
context.report(node, message || `Invalid "${ callee.name }()" description found.`);
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions test/rules/valid-test-description.js
Expand Up @@ -31,6 +31,22 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
options: [ '^should', [ 'someFunction' ] ],
code: 'someFunction("should do something", function () { });'
},
{
options: [ '^should', [ 'someFunction' ], 'some error message' ],
code: 'someFunction("should do something", function () { });'
},
{
options: [ /^should/, [ 'someFunction' ], 'some error message' ],
code: 'someFunction("should do something", function () { });'
},
{
options: [ { pattern: '^should', testNames: [ 'someFunction' ], message: 'some error message' } ],
code: 'someFunction("should do something", function () { });'
},
{
options: [ { pattern: /^should/, testNames: [ 'someFunction' ], message: 'some error message' } ],
code: 'someFunction("should do something", function () { });'
},
'someOtherFunction();',
{
parserOptions: { ecmaVersion: 2017 },
Expand Down Expand Up @@ -84,6 +100,27 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
errors: [
{ message: 'Invalid "customFunction()" description found.' }
]
},
{
options: [ 'required', [ 'customFunction' ], 'some error message' ],
code: 'customFunction("this is a test", function () { });',
errors: [
{ message: 'some error message' }
]
},
{
options: [ { pattern: 'required', testNames: [ 'customFunction' ], message: 'some error message' } ],
code: 'customFunction("this is a test", function () { });',
errors: [
{ message: 'some error message' }
]
},
{
options: [ {} ],
code: 'it("this is a test", function () { });',
errors: [
{ message: 'Invalid "it()" description found.' }
]
}
]
});

0 comments on commit 91d38c3

Please sign in to comment.