-
Notifications
You must be signed in to change notification settings - Fork 236
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-test-prefixes rule (#70)
Fixes #63
- Loading branch information
Showing
5 changed files
with
147 additions
and
15 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,32 @@ | ||
# Use `.only` and `.skip` over `f` and `x` (no-test-prefixes) | ||
|
||
Jest allows you to choose how you want to define focused and skipped tests, with | ||
multiple permutations for each: | ||
|
||
* **only & skip:** `it.only`, `test.only`, `describe.only`, `it.skip`, | ||
`test.skip`, `describe.skip`. | ||
* **'f' & 'x':** `fit`, `fdescribe`, `xit`, `xtest`, `xdescribe`. | ||
|
||
This rule enforces usages from the **only & skip** list. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if you use one of the keywords from the **'f' & | ||
'x'** list to focus/skip a test. | ||
|
||
```js | ||
/*eslint jest/no-test-prefixes: "error"*/ | ||
|
||
it.only('foo'); // valid | ||
test.only('foo'); // valid | ||
describe.only('foo'); // valid | ||
it.skip('foo'); // valid | ||
test.skip('foo'); // valid | ||
describe.skip('foo'); // valid | ||
|
||
fit('foo'); // invalid | ||
fdescribe('foo'); // invalid | ||
xit('foo'); // invalid | ||
xtest('foo'); // invalid | ||
xdescribe('foo'); // invalid | ||
``` |
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,48 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-test-prefixes', rules['no-test-prefixes'], { | ||
valid: [ | ||
'describe("foo", function () {})', | ||
'it("foo", function () {})', | ||
'test("foo", function () {})', | ||
'describe.only("foo", function () {})', | ||
'it.only("foo", function () {})', | ||
'test.only("foo", function () {})', | ||
'describe.skip("foo", function () {})', | ||
'it.skip("foo", function () {})', | ||
'test.skip("foo", function () {})', | ||
'foo()', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'fdescribe("foo", function () {})', | ||
errors: [{ message: 'Use "describe.only" instead', column: 1, line: 1 }], | ||
output: 'describe.only("foo", function () {})', | ||
}, | ||
{ | ||
code: 'fit("foo", function () {})', | ||
errors: [{ message: 'Use "it.only" instead', column: 1, line: 1 }], | ||
output: 'it.only("foo", function () {})', | ||
}, | ||
{ | ||
code: 'xdescribe("foo", function () {})', | ||
errors: [{ message: 'Use "describe.skip" instead', column: 1, line: 1 }], | ||
output: 'describe.skip("foo", function () {})', | ||
}, | ||
{ | ||
code: 'xit("foo", function () {})', | ||
errors: [{ message: 'Use "it.skip" instead', column: 1, line: 1 }], | ||
output: 'it.skip("foo", function () {})', | ||
}, | ||
{ | ||
code: 'xtest("foo", function () {})', | ||
errors: [{ message: 'Use "test.skip" instead', column: 1, line: 1 }], | ||
output: 'test.skip("foo", function () {})', | ||
}, | ||
], | ||
}); |
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,49 @@ | ||
'use strict'; | ||
|
||
const getNodeName = require('./util').getNodeName; | ||
const isTestCase = require('./util').isTestCase; | ||
const isDescribe = require('./util').isDescribe; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-test-prefixes.md', | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression: node => { | ||
const nodeName = getNodeName(node.callee); | ||
|
||
if (!isDescribe(node) && !isTestCase(node)) return; | ||
|
||
const preferredNodeName = getPreferredNodeName(nodeName); | ||
|
||
if (!preferredNodeName) return; | ||
|
||
context.report({ | ||
message: 'Use "{{ preferredNodeName }}" instead', | ||
node: node.callee, | ||
data: { preferredNodeName }, | ||
fix(fixer) { | ||
return [fixer.replaceText(node.callee, preferredNodeName)]; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function getPreferredNodeName(nodeName) { | ||
const firstChar = nodeName.charAt(0); | ||
|
||
if (firstChar === 'f') { | ||
return nodeName.slice(1) + '.only'; | ||
} | ||
|
||
if (firstChar === 'x') { | ||
return nodeName.slice(1) + '.skip'; | ||
} | ||
} |