Skip to content

Commit 5bd8f61

Browse files
authored
feat: support allowedPrefixes option in lowercase-name rule (#419)
1 parent ad3fc06 commit 5bd8f61

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

docs/rules/lowercase-name.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,19 @@ Example of **correct** code for the `{ "ignore": ["it"] }` option:
7070

7171
it('Uppercase description');
7272
```
73+
74+
### `allow`
75+
76+
This array option whitelists prefixes that titles can start with with capitals.
77+
This can be useful when writing tests for api endpoints, where you'd like to
78+
prefix with the HTTP method.
79+
80+
By default, nothing is allowed (the equivalent of `{ "allow": [] }`).
81+
82+
Example of **correct** code for the `{ "allow": ["GET"] }` option:
83+
84+
```js
85+
/* eslint jest/lowercase-name: ["error", { "allow": ["GET"] }] */
86+
87+
describe('GET /live');
88+
```

src/rules/__tests__/lowercase-name.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ ruleTester.run('lowercase-name', rule, {
4646
'describe(``)',
4747
'describe("")',
4848
'describe(42)',
49+
{
50+
code: 'describe(42)',
51+
options: [{ ignore: undefined, allowedPrefixes: undefined }],
52+
},
4953
],
5054

5155
invalid: [
@@ -225,3 +229,21 @@ ruleTester.run('lowercase-name with ignore=it', rule, {
225229
],
226230
invalid: [],
227231
});
232+
233+
ruleTester.run('lowercase-name with allowedPrefixes', rule, {
234+
valid: [
235+
{
236+
code: "it('GET /live', function () {})",
237+
options: [{ allowedPrefixes: ['GET'] }],
238+
},
239+
{
240+
code: 'it("POST /live", function () {})',
241+
options: [{ allowedPrefixes: ['GET', 'POST'] }],
242+
},
243+
{
244+
code: 'it(`PATCH /live`, function () {})',
245+
options: [{ allowedPrefixes: ['GET', 'PATCH'] }],
246+
},
247+
],
248+
invalid: [],
249+
});

src/rules/lowercase-name.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ const testDescription = (argument: ArgumentLiteral): string | null => {
5454

5555
const jestFunctionName = (
5656
node: CallExpressionWithCorrectCalleeAndArguments,
57+
allowedPrefixes: readonly string[],
5758
) => {
5859
const description = testDescription(node.arguments[0]);
59-
if (description === null) {
60+
if (
61+
description === null ||
62+
allowedPrefixes.some(name => description.startsWith(name))
63+
) {
6064
return null;
6165
}
6266

@@ -73,7 +77,15 @@ const jestFunctionName = (
7377
return null;
7478
};
7579

76-
export default createRule({
80+
export default createRule<
81+
[
82+
Partial<{
83+
ignore: readonly JestFunctionName[];
84+
allowedPrefixes: readonly string[];
85+
}>,
86+
],
87+
'unexpectedLowercase'
88+
>({
7789
name: __filename,
7890
meta: {
7991
type: 'suggestion',
@@ -96,19 +108,24 @@ export default createRule({
96108
items: { enum: ['describe', 'test', 'it'] },
97109
additionalItems: false,
98110
},
111+
allowedPrefixes: {
112+
type: 'array',
113+
items: { type: 'string' },
114+
additionalItems: false,
115+
},
99116
},
100117
additionalProperties: false,
101118
},
102119
],
103120
} as const,
104-
defaultOptions: [{ ignore: [] } as { ignore: readonly JestFunctionName[] }],
105-
create(context, [{ ignore }]) {
121+
defaultOptions: [{ ignore: [], allowedPrefixes: [] }],
122+
create(context, [{ ignore = [], allowedPrefixes = [] }]) {
106123
return {
107124
CallExpression(node) {
108125
if (!isJestFunctionWithLiteralArg(node)) {
109126
return;
110127
}
111-
const erroneousMethod = jestFunctionName(node);
128+
const erroneousMethod = jestFunctionName(node, allowedPrefixes);
112129

113130
if (erroneousMethod && !ignore.includes(node.callee.name)) {
114131
context.report({

0 commit comments

Comments
 (0)