-
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 #304 from pustovitDmytro/master
Add `no-empty-description` rule. closes #302
- Loading branch information
Showing
6 changed files
with
218 additions
and
4 deletions.
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
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,45 @@ | ||
# Disallow empty test descriptions (no-empty-description) | ||
|
||
This rule enforces you to specify the suite/test descriptions for each test. | ||
|
||
## Rule Details | ||
|
||
This rule checks each mocha test function to have a non-empty description. | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
it(); | ||
|
||
suite(""); | ||
|
||
test(function() { }) | ||
|
||
test.only(" ", function() { }) | ||
|
||
``` | ||
|
||
These patterns would not be considered problems: | ||
|
||
```js | ||
describe('foo', function () { | ||
it('bar'); | ||
}); | ||
|
||
suite('foo', function () { | ||
test('bar'); | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
Example of a custom rule configuration: | ||
|
||
```js | ||
rules: { | ||
"mocha/no-empty-description": [ "warn", { | ||
testNames: ["it", "specify", "test", "mytestname"], | ||
message: 'custom error message' | ||
} ] | ||
} | ||
``` |
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,82 @@ | ||
'use strict'; | ||
|
||
const { getStringIfConstant } = require('eslint-utils'); | ||
|
||
const DEFAULT_TEST_NAMES = [ 'describe', 'context', 'suite', 'it', 'test', 'specify' ]; | ||
const ERROR_MESSAGE = 'Unexpected empty test description.'; | ||
|
||
function objectOptions(options = {}) { | ||
const { | ||
testNames = DEFAULT_TEST_NAMES, | ||
message | ||
} = options; | ||
|
||
return { testNames, message }; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow empty test descriptions', | ||
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-empty-description.md' | ||
}, | ||
messages: { | ||
error: ERROR_MESSAGE | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
testNames: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
}, | ||
message: { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
create(context) { | ||
const options = context.options[0]; | ||
|
||
const { testNames, message } = objectOptions(options); | ||
|
||
function isTest(node) { | ||
return node.callee && node.callee.name && testNames.includes(node.callee.name); | ||
} | ||
|
||
function isTemplateString(node) { | ||
return [ 'TaggedTemplateExpression', 'TemplateLiteral' ].includes(node && node.type); | ||
} | ||
|
||
function checkDescription(mochaCallExpression) { | ||
const description = mochaCallExpression.arguments[0]; | ||
const text = getStringIfConstant(description); | ||
|
||
if (isTemplateString(description) && text === null) { | ||
return true; | ||
} | ||
|
||
return text && text.trim().length; | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (isTest(node)) { | ||
if (!checkDescription(node)) { | ||
context.report({ | ||
node, | ||
message: message || ERROR_MESSAGE | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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,83 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
const ruleTester = new RuleTester(); | ||
const defaultErrorMessage = 'Unexpected empty test description.'; | ||
const firstLine = { column: 1, line: 1 }; | ||
|
||
ruleTester.run('no-empty-description', rules['no-empty-description'], { | ||
|
||
valid: [ | ||
'describe("some text")', | ||
'describe.only("some text")', | ||
'describe("some text", function() { })', | ||
|
||
'context("some text")', | ||
'context.only("some text")', | ||
'context("some text", function() { })', | ||
|
||
'it("some text")', | ||
'it.only("some text")', | ||
'it("some text", function() { })', | ||
|
||
'suite("some text")', | ||
'suite.only("some text")', | ||
'suite("some text", function() { })', | ||
|
||
'test("some text")', | ||
'test.only("some text")', | ||
'test("some text", function() { })', | ||
|
||
'notTest()', | ||
|
||
{ | ||
parserOptions: { ecmaVersion: 2019 }, | ||
code: 'it(string`template`, function () {});' | ||
}, | ||
{ | ||
parserOptions: { ecmaVersion: 2019 }, | ||
code: 'it(`template strings`, function () {});' | ||
}, | ||
{ | ||
parserOptions: { ecmaVersion: 2019 }, | ||
code: 'it(`${foo} template strings`, function () {});' | ||
}, | ||
{ | ||
options: [ { testNames: [ 'someFunction' ] } ], | ||
code: 'someFunction("this is a test", function () { });' | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'test()', | ||
errors: [ { message: defaultErrorMessage, ...firstLine } ] | ||
}, | ||
{ | ||
code: 'test(function() { })', | ||
errors: [ { message: defaultErrorMessage, ...firstLine } ] | ||
}, | ||
{ | ||
code: 'test("", function() { })', | ||
errors: [ { message: defaultErrorMessage, ...firstLine } ] | ||
}, | ||
{ | ||
code: 'test(" ", function() { })', | ||
errors: [ { message: defaultErrorMessage, ...firstLine } ] | ||
}, | ||
|
||
{ | ||
options: [ { testNames: [ 'someFunction' ], message: 'Custom Error' } ], | ||
code: 'someFunction(function() { })', | ||
errors: [ { message: 'Custom Error', ...firstLine } ] | ||
}, | ||
{ | ||
parserOptions: { ecmaVersion: 2019 }, | ||
code: 'it(` `, function () { });', | ||
errors: [ { message: defaultErrorMessage, ...firstLine } ] | ||
} | ||
] | ||
|
||
}); | ||
|