Skip to content

Commit

Permalink
feat: add lowercase-description rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail-syed committed Feb 11, 2018
1 parent eb0a2a2 commit 7d0f175
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ for more information about extending configuration files.
| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------------ | --------------------------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce consistent test or it keyword | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [lowercase-description](docs/rules/lowercase-description.md) | Disallow capitalized `it()` descriptions | | |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
Expand Down
21 changes: 21 additions & 0 deletions docs/rules/lowercase-description.md
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);
});
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const consistentTestIt = require('./rules/consistent-test-it');
const lowercaseDescription = require('./rules/lowercase-description');
const noDisabledTests = require('./rules/no-disabled-tests');
const noFocusedTests = require('./rules/no-focused-tests');
const noIdenticalTitle = require('./rules/no-identical-title');
Expand Down Expand Up @@ -59,6 +60,7 @@ module.exports = {
},
rules: {
'consistent-test-it': consistentTestIt,
'lowercase-description': lowercaseDescription,
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
Expand Down
79 changes: 79 additions & 0 deletions rules/__tests__/lowercase-description.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rules = require('../..').rules;

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
});

const errMessage =
'it(), test() and describe() descriptions should begin with lowercase';
const errors = [{ message: errMessage, column: 1, line: 1 }];

ruleTester.run('lowercase-description', rules['lowercase-description'], {
valid: [
"it(' ', function () {})",
'it(" ", function () {})',
'it(` `, function () {})',
"it('foo', function () {})",
'it("foo", function () {})',
'it(`foo`, function () {})',
'it("<Foo/>", function () {})',
'it("123 foo", function () {})',
'it(42, function () {})',
"test('foo', function () {})",
'test("foo", function () {})',
'test(`foo`, function () {})',
'test("<Foo/>", function () {})',
'test("123 foo", function () {})',
'test("42", function () {})',
"describe('foo', function () {})",
'describe("foo", function () {})',
'describe(`foo`, function () {})',
'describe("<Foo/>", function () {})',
'describe("123 foo", function () {})',
'describe("42", function () {})',
],

invalid: [
{
code: "it('Foo', function () {})",
errors,
},
{
code: 'it("Foo", function () {})',
errors,
},
{
code: 'it(`Foo`, function () {})',
errors,
},
{
code: "test('Foo', function () {})",
errors,
},
{
code: 'test("Foo", function () {})',
errors,
},
{
code: 'test(`Foo`, function () {})',
errors,
},
{
code: "describe('Foo', function () {})",
errors,
},
{
code: 'describe("Foo", function () {})',
errors,
},
{
code: 'describe(`Foo`, function () {})',
errors,
},
],
});
65 changes: 65 additions & 0 deletions rules/lowercase-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const ruleMsg =
'it(), test() and describe() descriptions should begin with lowercase';

const isItTestOrDescribeFunction = node => {
return (
node.type === 'CallExpression' &&
node.callee &&
(node.callee.name === 'it' ||
node.callee.name === 'test' ||
node.callee.name === 'describe')
);
};

const isItDescription = node => {
return (
node.arguments &&
node.arguments[0] &&
(node.arguments[0].type === 'Literal' ||
node.arguments[0].type === 'TemplateLiteral')
);
};

const testDescription = node => {
const type = node.arguments[0].type;
if (type === 'Literal') {
return node.arguments[0].value;
}
if (type === 'TemplateLiteral') {
return node.arguments[0].quasis[0].value.raw;
}
};

const descriptionBeginsWithLowerCase = node => {
if (isItTestOrDescribeFunction(node) && isItDescription(node)) {
const description = testDescription(node);
if (!description[0]) {
return true;
}
return description[0] === description[0].toLowerCase();
}
return true;
};

module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/lowercase-description.md',
},
},
create(context) {
return {
CallExpression(node) {
if (!descriptionBeginsWithLowerCase(node)) {
context.report({
message: ruleMsg,
node: node,
});
}
},
};
},
};

0 comments on commit 7d0f175

Please sign in to comment.