Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: support ignoreTypeOfDescribeName in valid-title
Fixes #431
  • Loading branch information
G-Rath committed Oct 27, 2019
1 parent 1275471 commit 7dec202
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
35 changes: 27 additions & 8 deletions docs/rules/valid-title.md
Expand Up @@ -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:

Expand All @@ -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**
Expand Down
36 changes: 36 additions & 0 deletions src/rules/__tests__/valid-title.test.ts
Expand Up @@ -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: [
{
Expand All @@ -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: [
Expand All @@ -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: [
Expand Down
22 changes: 18 additions & 4 deletions src/rules/valid-title.ts
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit 7dec202

Please sign in to comment.