Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rules): Add no-empty-title rule #238

Merged
merged 17 commits into from Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,7 @@ for more information about extending configuration files.
| [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
| [no-alias-methods][] | Disallow alias methods | ![recommended][] | ![fixable-green][] |
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
| [no-empty-title][] | Disallow disabled tests | ![recommended][] | |
SimenB marked this conversation as resolved.
Show resolved Hide resolved
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
| [no-hooks][] | Disallow setup and teardown hooks | | |
| [no-identical-title][] | Disallow identical titles | ![recommended][] | |
Expand Down Expand Up @@ -131,6 +132,7 @@ for more information about extending configuration files.
[lowercase-name]: docs/rules/lowercase-name.md
[no-alias-methods]: docs/rules/no-alias-methods.md
[no-disabled-tests]: docs/rules/no-disabled-tests.md
[no-empty-title]: docs/rules/no-empty-title.md
[no-focused-tests]: docs/rules/no-focused-tests.md
[no-hooks]: docs/rules/no-hooks.md
[no-identical-title]: docs/rules/no-identical-title.md
Expand Down
36 changes: 36 additions & 0 deletions docs/rules/no-empty-title.md
@@ -0,0 +1,36 @@
# Disallow empty titles

Having an empty string as your test title is pretty useless. This rule reports
an error if it finds an empty string as s test title.

This rule is not auto-fixable.

## Rule Details

The following patterns are considered warnings:

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

These patterns would not be considered warnings:

```js
describe('foo', () => {});
describe('foo', () => {
it('bar', () => {});
});
test('foo', () => {});
it('foo', () => {});
xdescribe('foo', () => {});
xit('foo', () => {});
xtest('foo', () => {});
```
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -42,6 +42,7 @@ module.exports = {
rules: {
'jest/no-alias-methods': 'warn',
'jest/no-disabled-tests': 'warn',
'jest/no-empty-title': 'error',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/no-jest-import': 'error',
Expand Down
96 changes: 96 additions & 0 deletions rules/__tests__/no-empty-title.js
@@ -0,0 +1,96 @@
'use strict';

const { RuleTester } = require('eslint');
const rule = require('../no-empty-title');

const ruleTester = new RuleTester({
parserOptions: {
sourceType: 'module',
},
});

ruleTester.run('no-empty-title', rule, {
valid: [
'someFn("", function () {})',
'describe(1, function () {})',
'describe("foo", function () {})',
'describe("foo", function () { it("bar", function () {}) })',
'test("foo", function () {})',
"it('foo', function () {})",
"xdescribe('foo', function () {})",
"xit('foo', function () {})",
"xtest('foo', function () {})",
],
invalid: [
{
code: 'describe("", function () {})',
errors: [
{
message: rule.errorMessages.describe,
column: 1,
line: 1,
},
],
},
{
code: ["describe('foo', () => {", "it('', () => {})", '})'].join('\n'),
errors: [
{
message: rule.errorMessages.test,
column: 1,
line: 2,
},
],
},
{
code: 'it("", function () {})',
errors: [
{
message: rule.errorMessages.test,
column: 1,
line: 1,
},
],
},
{
code: 'test("", function () {})',
errors: [
{
message: rule.errorMessages.test,
column: 1,
line: 1,
},
],
},
{
code: "xdescribe('', () => {})",
errors: [
{
message: rule.errorMessages.describe,
column: 1,
line: 1,
},
],
},
{
code: "xit('', () => {})",
errors: [
{
message: rule.errorMessages.test,
column: 1,
line: 1,
},
],
},
{
code: "xtest('', () => {})",
errors: [
{
message: rule.errorMessages.test,
column: 1,
line: 1,
},
],
},
],
});
43 changes: 43 additions & 0 deletions rules/no-empty-title.js
@@ -0,0 +1,43 @@
'use strict';

const { getDocsUrl, isDescribe, isTestCase, isString } = require('./util');

const errorMessages = {
describe: 'describe should not have an empty title',
test: 'test should not have an empty title',
};

module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
},
create(context) {
return {
CallExpression(node) {
const is = {
describe: isDescribe(node),
testCase: isTestCase(node),
};
if (!is.describe && !is.testCase) {
return;
}
const [firstArgument] = node.arguments;
if (!isString(firstArgument)) {
return;
}
if (firstArgument.value === '') {
const message = is.describe
? errorMessages.describe
: errorMessages.test;
context.report({
message,
node,
});
}
},
};
},
errorMessages,
};