-
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.
- Loading branch information
Showing
5 changed files
with
123 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 `toBeDefined()` (prefer-to-be-defined) | ||
|
||
In order to have a better failure message, `toBeDefined()` should be used upon asserting expections on defined value. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toBe()` is used to assert a undefined value. | ||
|
||
```js | ||
expect(true).not.toBe(undefined); | ||
``` | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warning: | ||
|
||
```js | ||
expect(true).not.toBe(undefined); | ||
expect(true).not.toBeUndefined(); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(true).toBeDefined(); | ||
``` |
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,35 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const { rules } = require('../../'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer_to_be_defined', rules['prefer-to-be-defined'], { | ||
valid: ['expect(true).toBeDefined();'], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(true).not.toBe(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeDefined() instead', | ||
column: 14, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(true).toBeDefined();', | ||
}, | ||
{ | ||
code: 'expect(true).not.toBeUndefined();', | ||
errors: [ | ||
{ | ||
message: 'Use toBeDefined() instead', | ||
column: 14, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(true).toBeDefined();', | ||
}, | ||
], | ||
}); |
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,53 @@ | ||
'use strict'; | ||
|
||
module.exports = context => { | ||
return { | ||
CallExpression(node) { | ||
const calleeName = node.callee.name; | ||
|
||
if ( | ||
calleeName === 'expect' && | ||
node.arguments.length == 1 && | ||
node.parent && | ||
node.parent.type === 'MemberExpression' && | ||
node.parent.parent && | ||
node.parent.parent.type === 'MemberExpression' | ||
) { | ||
const parentProperty = node.parent.property; | ||
const propertyName = parentProperty.name; | ||
const parentProperty2 = node.parent.parent.property; | ||
const propertyName2 = parentProperty2.name; | ||
const argument = node.parent.parent.parent.arguments[0]; | ||
const propertyDot = context | ||
.getSourceCode() | ||
.getFirstTokenBetween( | ||
parentProperty, | ||
parentProperty2, | ||
token => token.value === '.' | ||
); | ||
if ( | ||
(propertyName === 'not' && | ||
propertyName2 === 'toBe' && | ||
argument.value === undefined) || | ||
(propertyName === 'not' && propertyName2 === 'toBeUndefined') | ||
) { | ||
context.report({ | ||
fix(fixer) { | ||
const fixes = [ | ||
fixer.remove(parentProperty), | ||
fixer.remove(propertyDot), | ||
fixer.replaceText(parentProperty2, 'toBeDefined'), | ||
]; | ||
if (argument) { | ||
fixes.push(fixer.remove(argument)); | ||
} | ||
return fixes; | ||
}, | ||
message: 'Use toBeDefined() instead', | ||
node: parentProperty, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |