-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): Add no-empty-title rule (#238)
Fixes #226
- Loading branch information
1 parent
f6f6d84
commit c793b7a
Showing
7 changed files
with
216 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', () => {}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'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 () {})', | ||
'test(`foo`, 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: '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, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
const { | ||
getDocsUrl, | ||
hasExpressions, | ||
isDescribe, | ||
isTestCase, | ||
isTemplateLiteral, | ||
isString, | ||
getStringValue, | ||
} = 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 (isTemplateLiteral(firstArgument) && hasExpressions(firstArgument)) { | ||
return; | ||
} | ||
if (getStringValue(firstArgument) === '') { | ||
const message = is.describe | ||
? errorMessages.describe | ||
: errorMessages.test; | ||
context.report({ | ||
message, | ||
node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
errorMessages, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters