This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Seccafien
committed
Mar 22, 2019
1 parent
ed1239b
commit bdf93cd
Showing
4 changed files
with
371 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Prevent if statements in tests. (jest/no-if) | ||
This rule prevents the use of if statements in tests. | ||
|
||
## Rule Details | ||
|
||
If statments in tests is usually an indication that a test is attempting to cover too much. Each branch of code executing within an if statement will likely be better served by a test devoted to it. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
it('foo', () => { | ||
if('bar') { | ||
// an if statement here is invalid | ||
// you are probably testing too much | ||
} | ||
}) | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
it('foo', () => { | ||
// only test the 'foo' case | ||
}) | ||
|
||
it('bar', () => { | ||
// test the 'bar' case separately | ||
}) | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not wish to prevent the use of if statements in tests, you can safely disable this rule. |
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,93 @@ | ||
const {docsUrl} = require('../../utilities'); | ||
|
||
const TEST_FUNCTION_NAMES = [ | ||
'it', | ||
'xit', | ||
'fit', | ||
'test', | ||
'xtest', | ||
'describe', | ||
'fdescribe', | ||
'xdescribe', | ||
]; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prevent if statements in tests.', | ||
category: 'Best Practices', | ||
recommended: false, | ||
uri: docsUrl('jest/no-if'), | ||
}, | ||
messages: { | ||
noIf: [ | ||
'Tests should not contain if statements.', | ||
'This is usually an indication that you', | ||
'are attempting to test too much at once', | ||
'and may want to consider breaking the if', | ||
'statement out into a separate test.', | ||
].join(' '), | ||
}, | ||
}, | ||
|
||
create(context) { | ||
let inCallExpression = false; | ||
|
||
return { | ||
CallExpression(node) { | ||
if (notTestFunction(node) || hasEmptyBody(node)) { | ||
return; | ||
} | ||
inCallExpression = true; | ||
}, | ||
IfStatement(node) { | ||
if (!inCallExpression) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
messageId: 'noIf', | ||
node, | ||
}); | ||
}, | ||
'CallExpression:exit': () => { | ||
inCallExpression = false; | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function notTestFunction(node) { | ||
const method = getMethodName(node); | ||
return !matchTestFunctionName(method); | ||
} | ||
|
||
function matchTestFunctionName(functionName) { | ||
return TEST_FUNCTION_NAMES.includes(functionName); | ||
} | ||
|
||
function hasEmptyBody(node) { | ||
return ( | ||
node.arguments && | ||
node.arguments.length === 2 && | ||
node.arguments[1].type === 'ArrowFunctionExpression' && | ||
node.arguments[1].body && | ||
node.arguments[1].body.body && | ||
node.arguments[1].body.body.length === 0 | ||
); | ||
} | ||
|
||
function getMethodName({callee}) { | ||
switch (callee.type) { | ||
case 'CallExpression': | ||
return callee.callee.object | ||
? callee.callee.object.name | ||
: callee.callee.name; | ||
case 'Identifier': | ||
return callee.name; | ||
case 'MemberExpression': | ||
return callee.object.name; | ||
default: | ||
return false; | ||
} | ||
} |
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,240 @@ | ||
const {RuleTester} = require('eslint'); | ||
const rule = require('../../../../lib/rules/jest/no-if'); | ||
require('babel-eslint'); | ||
|
||
const parser = 'babel-eslint'; | ||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-if', rule, { | ||
valid: [ | ||
{ | ||
code: `if(foo) {}`, | ||
}, | ||
{ | ||
code: `it('foo', () => {})`, | ||
parser, | ||
}, | ||
{ | ||
code: `foo('bar', () => { | ||
if(baz) {} | ||
})`, | ||
parser, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `it('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `it.skip('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `it.only('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `xit('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `fit('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe.skip('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe.only('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `xdescribe.only('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `fdescribe.only('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `test('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `test.skip('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `test.only('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `xtest('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `ftest('foo', () => { | ||
if('bar') {} | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe('foo', () => { | ||
it('bar', () => { | ||
if('bar') {} | ||
}) | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe('foo', () => { | ||
it('bar', () => { | ||
if('bar') {} | ||
}) | ||
it('baz', () => { | ||
if('qux') {} | ||
if('quux') {} | ||
}) | ||
})`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `describe('foo', () => { | ||
if('bar') {} | ||
}) | ||
if('baz') {} | ||
`, | ||
parser, | ||
errors: [ | ||
{ | ||
messageId: 'noIf', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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