Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/rules/valid-test-description.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.' }
]
}
]
});