From 625b8d25af97010cd5e2c71d198dfb8bfdb239c3 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 29 Sep 2019 14:45:45 +1300 Subject: [PATCH] feat: support `allow` option in `lowercase-name` rule --- docs/rules/lowercase-name.md | 16 ++++++++++++++ src/rules/__tests__/lowercase-name.test.ts | 22 +++++++++++++++++++ .../__tests__/valid-expect-in-promise.test.ts | 1 + src/rules/lowercase-name.ts | 22 ++++++++++++++----- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/rules/lowercase-name.md b/docs/rules/lowercase-name.md index 0e0f4fd85..8c653a986 100644 --- a/docs/rules/lowercase-name.md +++ b/docs/rules/lowercase-name.md @@ -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'); +``` diff --git a/src/rules/__tests__/lowercase-name.test.ts b/src/rules/__tests__/lowercase-name.test.ts index bbf68cb14..e33d2916f 100644 --- a/src/rules/__tests__/lowercase-name.test.ts +++ b/src/rules/__tests__/lowercase-name.test.ts @@ -46,6 +46,10 @@ ruleTester.run('lowercase-name', rule, { 'describe(``)', 'describe("")', 'describe(42)', + { + code: 'describe(42)', + options: [{ ignore: undefined, allow: undefined }], + }, ], invalid: [ @@ -225,3 +229,21 @@ ruleTester.run('lowercase-name with ignore=it', rule, { ], invalid: [], }); + +ruleTester.run('lowercase-name with allow', rule, { + valid: [ + { + code: "it('GET /live', function () {})", + options: [{ allow: ['GET'] }], + }, + { + code: 'it("POST /live", function () {})', + options: [{ allow: ['GET', 'POST'] }], + }, + { + code: 'it(`PATCH /live`, function () {})', + options: [{ allow: ['GET', 'PATCH'] }], + }, + ], + invalid: [], +}); diff --git a/src/rules/__tests__/valid-expect-in-promise.test.ts b/src/rules/__tests__/valid-expect-in-promise.test.ts index 9808d6582..448f566f9 100644 --- a/src/rules/__tests__/valid-expect-in-promise.test.ts +++ b/src/rules/__tests__/valid-expect-in-promise.test.ts @@ -9,6 +9,7 @@ const ruleTester = new TSESLint.RuleTester({ ruleTester.run('valid-expect-in-promise', rule, { valid: [ + `it('it1', async () => expect(await 1).toBe(1))`, ` it('it1', () => new Promise((done) => { test() diff --git a/src/rules/lowercase-name.ts b/src/rules/lowercase-name.ts index 367a9812f..6f11311c1 100644 --- a/src/rules/lowercase-name.ts +++ b/src/rules/lowercase-name.ts @@ -54,9 +54,13 @@ const testDescription = (argument: ArgumentLiteral): string | null => { const jestFunctionName = ( node: CallExpressionWithCorrectCalleeAndArguments, + allowed: readonly string[], ) => { const description = testDescription(node.arguments[0]); - if (description === null) { + if ( + description === null || + allowed.some(name => description.startsWith(name)) + ) { return null; } @@ -73,7 +77,10 @@ const jestFunctionName = ( return null; }; -export default createRule({ +export default createRule< + [Partial<{ ignore: readonly JestFunctionName[]; allow: readonly string[] }>], + 'unexpectedLowercase' +>({ name: __filename, meta: { type: 'suggestion', @@ -96,19 +103,24 @@ export default createRule({ items: { enum: ['describe', 'test', 'it'] }, additionalItems: false, }, + allow: { + type: 'array', + items: { type: 'string' }, + additionalItems: false, + }, }, additionalProperties: false, }, ], } as const, - defaultOptions: [{ ignore: [] } as { ignore: readonly JestFunctionName[] }], - create(context, [{ ignore }]) { + defaultOptions: [{ ignore: [], allow: [] }], + create(context, [{ ignore = [], allow = [] }]) { return { CallExpression(node) { if (!isJestFunctionWithLiteralArg(node)) { return; } - const erroneousMethod = jestFunctionName(node); + const erroneousMethod = jestFunctionName(node, allow); if (erroneousMethod && !ignore.includes(node.callee.name)) { context.report({