Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore: merge no-empty-title into valid-title
BREAKING CHANEG: delete `no-empty-title` - use `valid-title` instead
  • Loading branch information
G-Rath committed Oct 27, 2019
1 parent 2ce233c commit bb2d09e
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 145 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -117,7 +117,6 @@ installations requiring long-term consistency.
| [no-commented-out-tests][] | Disallow commented out tests | | |
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
| [no-duplicate-hooks][] | Disallow duplicate hooks within a `describe` block | | |
| [no-empty-title][] | Disallow empty titles | | |
| [no-expect-resolves][] | Disallow using `expect().resolves` | | |
| [no-export][] | Disallow export from test files | | |
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
Expand Down Expand Up @@ -173,7 +172,6 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
[no-commented-out-tests]: docs/rules/no-commented-out-tests.md
[no-disabled-tests]: docs/rules/no-disabled-tests.md
[no-duplicate-hooks]: docs/rules/no-duplicate-hooks.md
[no-empty-title]: docs/rules/no-empty-title.md
[no-expect-resolves]: docs/rules/no-expect-resolves.md
[no-export]: docs/rules/no-export.md
[no-focused-tests]: docs/rules/no-focused-tests.md
Expand Down
36 changes: 0 additions & 36 deletions docs/rules/no-empty-title.md

This file was deleted.

33 changes: 33 additions & 0 deletions docs/rules/valid-title.md
Expand Up @@ -2,11 +2,44 @@

Checks that the title of Jest blocks are valid by ensuring that titles are:

- not empty,
- not prefixed with their block name,
- have no leading or trailing spaces

## Rule Details

**emptyTitle**

An empty title is not informative, and serves little purpose.

Examples of **incorrect** code for this rule:

```js
describe('', () => {});
describe('foo', () => {
it('', () => {});
});
it('', () => {});
test('', () => {});
xdescribe('', () => {});
xit('', () => {});
xtest('', () => {});
```

Examples of **correct** code for this rule:

```js
describe('foo', () => {});
describe('foo', () => {
it('bar', () => {});
});
test('foo', () => {});
it('foo', () => {});
xdescribe('foo', () => {});
xit('foo', () => {});
xtest('foo', () => {});
```

**duplicatePrefix**

A describe/ test block should not start with duplicatePrefix
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'path';
import plugin from '../';

const ruleNames = Object.keys(plugin.rules);
const numberOfRules = 41;
const numberOfRules = 40;

describe('rules', () => {
it('should have a corresponding doc for each rule', () => {
Expand Down
59 changes: 0 additions & 59 deletions src/rules/__tests__/no-empty-title.test.ts

This file was deleted.

108 changes: 107 additions & 1 deletion src/rules/__tests__/valid-title.test.ts
Expand Up @@ -7,11 +7,117 @@ const ruleTester = new TSESLint.RuleTester({
},
});

ruleTester.run('no-empty-title', rule, {
valid: [
'describe()',
'someFn("", function () {})',
'describe(1, function () {})',
'describe("foo", function () {})',
'describe("foo", function () { it("bar", function () {}) })',
'test("foo", function () {})',
'test(`foo`, function () {})',
'test(`${foo}`, function () {})',
"it('foo', function () {})",
"xdescribe('foo', function () {})",
"xit('foo', function () {})",
"xtest('foo', function () {})",
],
invalid: [
{
code: 'describe("", function () {})',
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'describe' },
},
],
},
{
code: ["describe('foo', () => {", "it('', () => {})", '})'].join('\n'),
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 2,
data: { jestFunctionName: 'test' },
},
],
},
{
code: 'it("", function () {})',
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'test' },
},
],
},
{
code: 'test("", function () {})',
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'test' },
},
],
},
{
code: 'test(``, function () {})',
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'test' },
},
],
},
{
code: "xdescribe('', () => {})",
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'describe' },
},
],
},
{
code: "xit('', () => {})",
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'test' },
},
],
},
{
code: "xtest('', () => {})",
errors: [
{
messageId: 'emptyTitle',
column: 1,
line: 1,
data: { jestFunctionName: 'test' },
},
],
},
],
});

ruleTester.run('no-accidental-space', rule, {
valid: [
'it()',
'describe()',
'it("")',
'it.each()()',
'describe("foo", function () {})',
'describe(6, function () {})',
Expand Down
46 changes: 0 additions & 46 deletions src/rules/no-empty-title.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/rules/valid-title.ts
Expand Up @@ -3,6 +3,8 @@ import {
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
DescribeAlias,
TestCaseName,
createRule,
getNodeName,
getStringValue,
Expand All @@ -23,6 +25,7 @@ export default createRule({
recommended: false,
},
messages: {
emptyTitle: '{{ jestFunctionName }} should not have an empty title',
duplicatePrefix: 'should not have duplicate prefix',
accidentalSpace: 'should not have leading or trailing spaces',
},
Expand All @@ -47,6 +50,18 @@ export default createRule({
const title = getStringValue(argument);

if (!title) {
if (typeof title === 'string') {
context.report({
messageId: 'emptyTitle',
data: {
jestFunctionName: isDescribe(node)
? DescribeAlias.describe
: TestCaseName.test,
},
node,
});
}

return;
}

Expand Down

5 comments on commit bb2d09e

@dylanwulf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't listed as a breaking change on the releases page

@G-Rath
Copy link
Collaborator Author

@G-Rath G-Rath commented on bb2d09e Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylanwulf Correct - that is because of a typo I made in the commit.

@SimenB
Copy link
Member

@SimenB SimenB commented on bb2d09e Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can update the release and changelog though. @G-Rath wanna do it?

EDIT: It probably isn't in the changelog sa I haven't gotten around to updating it with old releases yet

@G-Rath
Copy link
Collaborator Author

@G-Rath G-Rath commented on bb2d09e Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB Good thinking - I'll do it now.

I'm a little confused to why it didn't appear in the CHANGELOG for 23.0.0 at all, as that's got a chore title 🤔

@SimenB
Copy link
Member

@SimenB SimenB commented on bb2d09e Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems like chore updates are not included in releases, so we'll have to use another notation.

Please sign in to comment.