-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16889bb
commit f213dd3
Showing
5 changed files
with
113 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Enforce lowercase descriptions in it() (lowercase-description) | ||
|
||
## Rule details | ||
|
||
Enforce `it()` tests to have descriptions that begin with a lowercase letter. This provides more readable test failures. This rule is not enabled by default. | ||
|
||
The following pattern is considered a warning: | ||
|
||
```js | ||
it('Adds 1 + 2 to equal 3', () => { | ||
expect(sum(1, 2)).toBe(3); | ||
}); | ||
``` | ||
|
||
The following pattern is not considered a warning: | ||
|
||
```js | ||
it('adds 1 + 2 to equal 3', () => { | ||
expect(sum(1, 2)).toBe(3); | ||
}); | ||
``` |
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,40 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
}); | ||
|
||
const errMessage = 'it() description should begin with lowercase'; | ||
const errors = [{ message: errMessage, column: 1, line: 1 }]; | ||
|
||
ruleTester.run('lowercase-description', rules['lowercase-description'], { | ||
valid: [ | ||
"it('foo', function () {})", | ||
'it("foo", function () {})', | ||
'it(`foo`, function () {})', | ||
'it("<Foo/>", function () {})', | ||
'it("123 foo", function () {})', | ||
'describe("foo", function () {})', | ||
'test("foo", function () {})', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "it('Foo', function () {})", | ||
errors, | ||
}, | ||
{ | ||
code: 'it("Foo", function () {})', | ||
errors, | ||
}, | ||
{ | ||
code: 'it(`Foo`, function () {})', | ||
errors, | ||
}, | ||
], | ||
}); |
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,49 @@ | ||
'use strict'; | ||
|
||
const ruleMsg = 'it() description should begin with lowercase'; | ||
|
||
const isItFunction = node => { | ||
return ( | ||
node.type === 'CallExpression' && node.callee && node.callee.name === 'it' | ||
); | ||
}; | ||
|
||
const isItDescription = node => { | ||
const foo = | ||
node.arguments && | ||
node.arguments[0] && | ||
(node.arguments[0].type === 'Literal' || | ||
node.arguments[0].type === 'TemplateLiteral'); | ||
return foo; | ||
}; | ||
|
||
const testDescription = node => { | ||
const { type } = node.arguments[0]; | ||
if (type === 'Literal') { | ||
return node.arguments[0].value; | ||
} | ||
if (type === 'TemplateLiteral') { | ||
return node.arguments[0].quasis[0].value.raw; | ||
} | ||
}; | ||
|
||
const descriptionBeginsWithLowerCase = node => { | ||
if (isItFunction(node) && isItDescription(node)) { | ||
const description = testDescription(node); | ||
return description[0] === description[0].toLowerCase(); | ||
} | ||
return true; | ||
}; | ||
|
||
module.exports = context => { | ||
return { | ||
CallExpression(node) { | ||
if (!descriptionBeginsWithLowerCase(node)) { | ||
context.report({ | ||
message: ruleMsg, | ||
node: node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}; |