Skip to content

Commit

Permalink
feat(lowercase-name): support ignoreTopLevelDescribe option
Browse files Browse the repository at this point in the history
closes #247
  • Loading branch information
G-Rath committed Jun 22, 2020
1 parent 7628266 commit c362bbf
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/rules/lowercase-name.md
Expand Up @@ -86,3 +86,21 @@ Example of **correct** code for the `{ "allowedPrefixes": ["GET"] }` option:

describe('GET /live');
```

### `ignoreTopLevelDescribe`

This option can be set to allow only the top-level `describe` blocks to have a
title starting with an upper-case letter.

Example of **correct** code for the `{ "ignoreTopLevelDescribe": true }` option:

```js
/* eslint jest/lowercase-name: ["error", { "ignoreTopLevelDescribe": true }] */
describe('MyClass', () => {
describe('#myMethod', () => {
it('does things', () => {
//
});
});
});
```
106 changes: 106 additions & 0 deletions src/rules/__tests__/lowercase-name.test.ts
@@ -1,4 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import dedent from 'dedent';
import resolveFrom from 'resolve-from';
import rule from '../lowercase-name';
import { DescribeAlias, TestCaseName } from '../utils';
Expand Down Expand Up @@ -251,3 +252,108 @@ ruleTester.run('lowercase-name with allowedPrefixes', rule, {
],
invalid: [],
});

ruleTester.run('lowercase-name with ignoreTopLevelDescribe', rule, {
valid: [
{
code: 'describe("MyClass", () => {});',
options: [{ ignoreTopLevelDescribe: true }],
},
{
code: dedent`
describe('MyClass', () => {
describe('#myMethod', () => {
it('does things', () => {
//
});
});
});
`,
options: [{ ignoreTopLevelDescribe: true }],
},
],
invalid: [
{
code: 'describe("myClass", () => {});',
options: [{ ignoreTopLevelDescribe: true }],
errors: [
{
messageId: 'unexpectedLowercase',
data: { method: DescribeAlias.describe },
column: 4,
line: 1,
},
],
},
{
code: 'it("Works!", () => {});',
options: [{ ignoreTopLevelDescribe: true }],
errors: [
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 4,
line: 1,
},
],
},
{
code: dedent`
describe('MyClass', () => {
describe('MyMethod', () => {
it('Does things', () => {
//
});
});
});
`,
options: [{ ignoreTopLevelDescribe: true }],
errors: [
{
messageId: 'unexpectedLowercase',
data: { method: DescribeAlias.describe },
column: 4,
line: 1,
},
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 4,
line: 1,
},
],
},
{
code: dedent`
describe('MyClass', () => {
describe('MyMethod', () => {
it('Does things', () => {
//
});
});
});
`,
options: [{ ignoreTopLevelDescribe: false }],
errors: [
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 4,
line: 1,
},
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 4,
line: 1,
},
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 4,
line: 1,
},
],
},
],
});
25 changes: 23 additions & 2 deletions src/rules/lowercase-name.ts
Expand Up @@ -62,6 +62,7 @@ export default createRule<
Partial<{
ignore: readonly IgnorableFunctionExpressions[];
allowedPrefixes: readonly string[];
ignoreTopLevelDescribe: boolean;
}>,
],
'unexpectedLowercase'
Expand Down Expand Up @@ -98,18 +99,38 @@ export default createRule<
items: { type: 'string' },
additionalItems: false,
},
ignoreTopLevelDescribe: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
],
} as const,
defaultOptions: [{ ignore: [], allowedPrefixes: [] }],
create(context, [{ ignore = [], allowedPrefixes = [] }]) {
defaultOptions: [
{ ignore: [], allowedPrefixes: [], ignoreTopLevelDescribe: false },
],
create(
context,
[{ ignore = [], allowedPrefixes = [], ignoreTopLevelDescribe }],
) {
let numberOfDescribeBlocks = 0;

return {
CallExpression(node: TSESTree.CallExpression) {
if (!isJestFunctionWithLiteralArg(node)) {
return;
}

if (isDescribe(node)) {
numberOfDescribeBlocks++;

if (ignoreTopLevelDescribe && numberOfDescribeBlocks === 1) {
return;
}
}

const erroneousMethod = jestFunctionName(node, allowedPrefixes);

if (erroneousMethod && !ignore.includes(node.callee.name)) {
Expand Down

0 comments on commit c362bbf

Please sign in to comment.