Skip to content
Permalink
Browse files
feat: support ignoreTypeOfDescribeName in valid-title
Fixes #431
  • Loading branch information
G-Rath committed Oct 27, 2019
1 parent 1275471 commit 7dec20228ae97e56ca87d39c6b0ee0e85e01fedf
Showing with 81 additions and 12 deletions.
  1. +27 −8 docs/rules/valid-title.md
  2. +36 −0 src/rules/__tests__/valid-title.test.ts
  3. +18 −4 src/rules/valid-title.ts
@@ -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**
@@ -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: [
@@ -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,

0 comments on commit 7dec202

Please sign in to comment.