Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support allow option in lowercase-name rule #419

Merged
merged 1 commit into from Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/rules/lowercase-name.md
Expand Up @@ -70,3 +70,19 @@ Example of **correct** code for the `{ "ignore": ["it"] }` option:

it('Uppercase description');
```

### `allow`

This array option whitelists prefixes that titles can start with with capitals.
This can be useful when writing tests for api endpoints, where you'd like to
prefix with the HTTP method.

By default, nothing is allowed (the equivalent of `{ "allow": [] }`).

Example of **correct** code for the `{ "allow": ["GET"] }` option:

```js
/* eslint jest/lowercase-name: ["error", { "allow": ["GET"] }] */

describe('GET /live');
```
22 changes: 22 additions & 0 deletions src/rules/__tests__/lowercase-name.test.ts
Expand Up @@ -46,6 +46,10 @@ ruleTester.run('lowercase-name', rule, {
'describe(``)',
'describe("")',
'describe(42)',
{
code: 'describe(42)',
options: [{ ignore: undefined, allowedPrefixes: undefined }],
},
],

invalid: [
Expand Down Expand Up @@ -225,3 +229,21 @@ ruleTester.run('lowercase-name with ignore=it', rule, {
],
invalid: [],
});

ruleTester.run('lowercase-name with allowedPrefixes', rule, {
valid: [
{
code: "it('GET /live', function () {})",
options: [{ allowedPrefixes: ['GET'] }],
},
{
code: 'it("POST /live", function () {})',
options: [{ allowedPrefixes: ['GET', 'POST'] }],
},
{
code: 'it(`PATCH /live`, function () {})',
options: [{ allowedPrefixes: ['GET', 'PATCH'] }],
},
],
invalid: [],
});
27 changes: 22 additions & 5 deletions src/rules/lowercase-name.ts
Expand Up @@ -54,9 +54,13 @@ const testDescription = (argument: ArgumentLiteral): string | null => {

const jestFunctionName = (
node: CallExpressionWithCorrectCalleeAndArguments,
allowedPrefixes: readonly string[],
) => {
const description = testDescription(node.arguments[0]);
if (description === null) {
if (
description === null ||
allowedPrefixes.some(name => description.startsWith(name))
) {
return null;
}

Expand All @@ -73,7 +77,15 @@ const jestFunctionName = (
return null;
};

export default createRule({
export default createRule<
[
Partial<{
ignore: readonly JestFunctionName[];
allowedPrefixes: readonly string[];
}>,
],
'unexpectedLowercase'
>({
name: __filename,
meta: {
type: 'suggestion',
Expand All @@ -96,19 +108,24 @@ export default createRule({
items: { enum: ['describe', 'test', 'it'] },
additionalItems: false,
},
allowedPrefixes: {
type: 'array',
items: { type: 'string' },
additionalItems: false,
},
},
additionalProperties: false,
},
],
} as const,
defaultOptions: [{ ignore: [] } as { ignore: readonly JestFunctionName[] }],
create(context, [{ ignore }]) {
defaultOptions: [{ ignore: [], allowedPrefixes: [] }],
create(context, [{ ignore = [], allowedPrefixes = [] }]) {
return {
CallExpression(node) {
if (!isJestFunctionWithLiteralArg(node)) {
return;
}
const erroneousMethod = jestFunctionName(node);
const erroneousMethod = jestFunctionName(node, allowedPrefixes);

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