-
Notifications
You must be signed in to change notification settings - Fork 238
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 prefer-todo rule (#218)
Fixes #217
- Loading branch information
1 parent
8dd5a80
commit 0933d82
Showing
6 changed files
with
183 additions
and
0 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,28 @@ | ||
# Suggest using `test.todo` (prefer-todo) | ||
|
||
When test cases are empty then it is better to mark them as `test.todo` as it | ||
will be highlighted in the summary output. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if empty test case is used without 'test.todo'. | ||
|
||
```js | ||
test('i need to write this test'); | ||
``` | ||
|
||
### Default configuration | ||
|
||
The following pattern is considered warning: | ||
|
||
```js | ||
test('i need to write this test'); // Unimplemented test case | ||
test('i need to write this test', () => {}); // Empty test case body | ||
test.skip('i need to write this test', () => {}); // Empty test case body | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
test.todo('i need to write this test'); | ||
``` |
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,59 @@ | ||
'use strict'; | ||
|
||
const { RuleTester } = require('eslint'); | ||
const rule = require('../prefer-todo'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2015 }, | ||
}); | ||
|
||
ruleTester.run('prefer-todo', rule, { | ||
valid: [ | ||
'test.todo("i need to write this test");', | ||
'test(obj)', | ||
'fit("foo")', | ||
'xit("foo")', | ||
'test("stub", () => expect(1).toBe(1));', | ||
` | ||
supportsDone && params.length < test.length | ||
? done => test(...params, done) | ||
: () => test(...params); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `test("i need to write this test");`, | ||
errors: [ | ||
{ message: 'Prefer todo test case over unimplemented test case' }, | ||
], | ||
output: 'test.todo("i need to write this test");', | ||
}, | ||
{ | ||
code: 'test(`i need to write this test`);', | ||
errors: [ | ||
{ message: 'Prefer todo test case over unimplemented test case' }, | ||
], | ||
output: 'test.todo(`i need to write this test`);', | ||
}, | ||
{ | ||
code: 'it("foo", function () {})', | ||
errors: ['Prefer todo test case over empty test case'], | ||
output: 'it.todo("foo")', | ||
}, | ||
{ | ||
code: 'it("foo", () => {})', | ||
errors: ['Prefer todo test case over empty test case'], | ||
output: 'it.todo("foo")', | ||
}, | ||
{ | ||
code: `test.skip("i need to write this test", () => {});`, | ||
errors: ['Prefer todo test case over empty test case'], | ||
output: 'test.todo("i need to write this test");', | ||
}, | ||
{ | ||
code: `test.skip("i need to write this test", function() {});`, | ||
errors: ['Prefer todo test case over empty test case'], | ||
output: 'test.todo("i need to write this test");', | ||
}, | ||
], | ||
}); |
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,78 @@ | ||
'use strict'; | ||
|
||
const { | ||
getDocsUrl, | ||
isFunction, | ||
composeFixers, | ||
getNodeName, | ||
isString, | ||
} = require('./util'); | ||
|
||
function isOnlyTestTitle(node) { | ||
return node.arguments.length === 1; | ||
} | ||
|
||
function isFunctionBodyEmpty(node) { | ||
return node.body.body && !node.body.body.length; | ||
} | ||
|
||
function isTestBodyEmpty(node) { | ||
const fn = node.arguments[1]; // eslint-disable-line prefer-destructuring | ||
return fn && isFunction(fn) && isFunctionBodyEmpty(fn); | ||
} | ||
|
||
function addTodo(node, fixer) { | ||
const testName = getNodeName(node.callee) | ||
.split('.') | ||
.shift(); | ||
return fixer.replaceText(node.callee, `${testName}.todo`); | ||
} | ||
|
||
function removeSecondArg({ arguments: [first, second] }, fixer) { | ||
return fixer.removeRange([first.range[1], second.range[1]]); | ||
} | ||
|
||
function isFirstArgString({ arguments: [firstArg] }) { | ||
return firstArg && isString(firstArg); | ||
} | ||
|
||
const isTestCase = node => | ||
node && | ||
node.type === 'CallExpression' && | ||
['it', 'test', 'it.skip', 'test.skip'].includes(getNodeName(node.callee)); | ||
|
||
function create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isTestCase(node) && isFirstArgString(node)) { | ||
const combineFixers = composeFixers(node); | ||
|
||
if (isTestBodyEmpty(node)) { | ||
context.report({ | ||
message: 'Prefer todo test case over empty test case', | ||
node, | ||
fix: combineFixers(removeSecondArg, addTodo), | ||
}); | ||
} | ||
|
||
if (isOnlyTestTitle(node)) { | ||
context.report({ | ||
message: 'Prefer todo test case over unimplemented test case', | ||
node, | ||
fix: combineFixers(addTodo), | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
fixable: 'code', | ||
}, | ||
}; |
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