-
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
8 changed files
with
181 additions
and
79 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
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 @@ | ||
# Suggest using `toBeDefined()` / `toBeUndefined()` (prefer-to-be-defined) | ||
|
||
In order to have a better failure message, `toBeDefined()` or `toBeUndefined()` | ||
should be used upon asserting expections on defined or undefined value. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `not.toBe()` is used to assert a undefined value | ||
or `toBe()` is used to assert a undefined value. | ||
|
||
```js | ||
expect(true).not.toBe(undefined); | ||
expect(undefined).toBe(undefined); | ||
``` | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warning: | ||
|
||
```js | ||
expect(true).not.toBeUndefined(); | ||
expect(undefined).toBe(undefined); | ||
``` | ||
|
||
The following patterns are not warning: | ||
|
||
```js | ||
expect(true).toBeDefined(); | ||
expect(undefined).toBeUndefined(); | ||
``` |
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,73 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../../').rules; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer_to_be_defined', rules['prefer-to-be-defined'], { | ||
valid: [ | ||
'expect(true).toBeDefined();', | ||
'expect(true).not.toBeDefined();', | ||
'expect(true).toBe(true);', | ||
'expect(undefined).toBeUndefined();', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(true).not.toBe(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeDefined() instead', | ||
column: 14, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(true).toBeDefined();', | ||
}, | ||
{ | ||
code: 'expect(true).not.toEqual(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();', | ||
}, | ||
{ | ||
code: 'expect(undefined).toBe(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeUndefined() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(undefined).toBeUndefined();', | ||
}, | ||
{ | ||
code: 'expect(undefined).toEqual(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeUndefined() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(undefined).toBeUndefined();', | ||
}, | ||
], | ||
}); |
This file was deleted.
Oops, something went wrong.
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 argument = require('./util').argument; | ||
const argument2 = require('./util').argument2; | ||
const expectNotToBeCase = require('./util').expectNotToBeCase; | ||
const expectNotToBeUndefinedCase = require('./util').expectNotToBeUndefinedCase; | ||
const expectNotToEqualCase = require('./util').expectNotToEqualCase; | ||
const expectToBeCase = require('./util').expectToBeCase; | ||
const expectToEqualCase = require('./util').expectToEqualCase; | ||
const method = require('./util').method; | ||
const method2 = require('./util').method2; | ||
|
||
module.exports = context => { | ||
return { | ||
CallExpression(node) { | ||
if ( | ||
expectNotToBeCase(node, undefined) || | ||
expectNotToEqualCase(node, undefined) || | ||
expectNotToBeUndefinedCase(node) | ||
) { | ||
context.report({ | ||
fix(fixer) { | ||
const propertyDot = context | ||
.getSourceCode() | ||
.getFirstTokenBetween( | ||
method(node), | ||
method2(node), | ||
token => token.value === '.' | ||
); | ||
const fixes = [ | ||
fixer.remove(method(node)), | ||
fixer.remove(propertyDot), | ||
fixer.replaceText(method2(node), 'toBeDefined'), | ||
]; | ||
if (argument2(node)) { | ||
fixes.push(fixer.remove(argument2(node))); | ||
} | ||
return fixes; | ||
}, | ||
message: 'Use toBeDefined() instead', | ||
node: method(node), | ||
}); | ||
} else if ( | ||
expectToBeCase(node, undefined) || | ||
expectToEqualCase(node, undefined) | ||
) { | ||
context.report({ | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceText(method(node), 'toBeUndefined'), | ||
fixer.remove(argument(node)), | ||
]; | ||
}, | ||
message: 'Use toBeUndefined() instead', | ||
node: method(node), | ||
}); | ||
} | ||
}, | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.