From 7dec20228ae97e56ca87d39c6b0ee0e85e01fedf Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 27 Oct 2019 13:46:19 +1300 Subject: [PATCH] feat: support `ignoreTypeOfDescribeName` in `valid-title` Fixes #431 --- docs/rules/valid-title.md | 35 ++++++++++++++++++------ src/rules/__tests__/valid-title.test.ts | 36 +++++++++++++++++++++++++ src/rules/valid-title.ts | 22 ++++++++++++--- 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/docs/rules/valid-title.md b/docs/rules/valid-title.md index 08ddebb40..579fdb48e 100644 --- a/docs/rules/valid-title.md +++ b/docs/rules/valid-title.md @@ -43,7 +43,10 @@ xtest('foo', () => {}); **titleMustBeString** -Titles should always be a string literal or expression. +Titles for test blocks should always be a string literal or expression. + +This is also applied to describe blocks by default, but can be turned off via +the `ignoreTypeOfDescribeName` option: Examples of **incorrect** code for this rule: @@ -52,18 +55,34 @@ it(123, () => {}); describe(String(/.+/), () => {}); describe(myFunction, () => {}); xdescribe(myFunction, () => {}); -describe(6, function () {}) +describe(6, function() {}); ``` Examples of **correct** code for this rule: ```js -it("is a string", () => {}); -test("is a string", () => {}); -xtest("is a string", () => {}); -describe("is a string", () => {}); -describe.skip("is a string", () => {}); -fdescribe("is a string", () => {}); +it('is a string', () => {}); +test('is a string', () => {}); +xtest('is a string', () => {}); +describe('is a string', () => {}); +describe.skip('is a string', () => {}); +fdescribe('is a string', () => {}); +``` + +Examples of **correct** code when `ignoreTypeOfDescribeName` is `true`: + +```js +it('is a string', () => {}); +test('is a string', () => {}); +xtest('is a string', () => {}); +describe('is a string', () => {}); +describe.skip('is a string', () => {}); +fdescribe('is a string', () => {}); + +describe(String(/.+/), () => {}); +describe(myFunction, () => {}); +xdescribe(myFunction, () => {}); +describe(6, function() {}); ``` **duplicatePrefix** diff --git a/src/rules/__tests__/valid-title.test.ts b/src/rules/__tests__/valid-title.test.ts index bfe06980e..b5a1ab539 100644 --- a/src/rules/__tests__/valid-title.test.ts +++ b/src/rules/__tests__/valid-title.test.ts @@ -12,9 +12,23 @@ ruleTester.run('title-must-be-string', rule, { 'it("is a string", () => {});', 'test("is a string", () => {});', 'xtest("is a string", () => {});', + 'xtest(`${myFunc} is a string`, () => {});', 'describe("is a string", () => {});', 'describe.skip("is a string", () => {});', + 'describe.skip(`${myFunc} is a string`, () => {});', 'fdescribe("is a string", () => {});', + { + code: 'describe(String(/.+/), () => {});', + options: [{ ignoreTypeOfDescribeName: true }], + }, + { + code: 'describe(myFunction, () => {});', + options: [{ ignoreTypeOfDescribeName: true }], + }, + { + code: 'xdescribe(skipFunction, () => {});', + options: [{ ignoreTypeOfDescribeName: true }], + }, ], invalid: [ { @@ -27,6 +41,17 @@ ruleTester.run('title-must-be-string', rule, { }, ], }, + { + code: 'test.skip(123, () => {});', + options: [{ ignoreTypeOfDescribeName: true }], + errors: [ + { + messageId: 'titleMustBeString', + column: 11, + line: 1, + }, + ], + }, { code: 'describe(String(/.+/), () => {});', errors: [ @@ -37,6 +62,17 @@ ruleTester.run('title-must-be-string', rule, { }, ], }, + { + code: 'describe(myFunction, () => 1);', + options: [{ ignoreTypeOfDescribeName: false }], + errors: [ + { + messageId: 'titleMustBeString', + column: 10, + line: 1, + }, + ], + }, { code: 'describe(myFunction, () => {});', errors: [ diff --git a/src/rules/valid-title.ts b/src/rules/valid-title.ts index 64fbe23b7..c0cd8ab3b 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -31,11 +31,22 @@ export default createRule({ accidentalSpace: 'should not have leading or trailing spaces', }, type: 'suggestion', - schema: [], + schema: [ + { + type: 'object', + properties: { + ignoreTypeOfDescribeName: { + type: 'boolean', + default: false, + }, + }, + additionalProperties: false, + }, + ], fixable: 'code', }, - defaultOptions: [], - create(context) { + defaultOptions: [{ ignoreTypeOfDescribeName: false }], + create(context, [{ ignoreTypeOfDescribeName }]) { return { CallExpression(node: TSESTree.CallExpression) { if (!(isDescribe(node) || isTestCase(node)) || !node.arguments.length) { @@ -45,7 +56,10 @@ export default createRule({ const [argument] = node.arguments; if (!isStringNode(argument)) { - if (argument.type !== AST_NODE_TYPES.TemplateLiteral) { + if ( + argument.type !== AST_NODE_TYPES.TemplateLiteral && + !(ignoreTypeOfDescribeName && isDescribe(node)) + ) { context.report({ messageId: 'titleMustBeString', loc: argument.loc,