-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(valid-expect): add valid-expect rule
- Loading branch information
Showing
5 changed files
with
121 additions
and
2 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,27 @@ | ||
# Enforce valid `expect()` usage (valid-expect) | ||
|
||
Ensure `expect()` is called with a single argument and there is an actual expectation made. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `expect()` is called with more than one argument or without arguments. | ||
It would also issue a warning if there is nothing called on `expect()`. | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect(); | ||
expect().toEqual("something"); | ||
expect("something", "else"); | ||
expect("something"); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
expect("something").toEqual("something"); | ||
``` |
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 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Enforce valid expect() usage | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
module.exports = function (context) { | ||
return { | ||
// checking "expect()" arguments | ||
CallExpression: function (node) { | ||
if (node.callee.name === 'expect') { | ||
if (node.arguments.length > 1) { | ||
context.report(node, 'More than one argument passed to expect()') | ||
} else if (node.arguments.length === 0) { | ||
context.report(node, 'No arguments passed to expect()') | ||
} | ||
} | ||
}, | ||
|
||
// nothing called on "expect()" | ||
'CallExpression:exit': function (node) { | ||
if (node.callee.name === 'expect' && node.parent.type === 'ExpressionStatement') { | ||
context.report(node, 'Nothing called on expect()') | ||
} | ||
} | ||
} | ||
} |
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,60 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/valid-expect') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('valid-expect', rule, { | ||
valid: [ | ||
'expect("something").toEqual("else");', | ||
'expect(true).toBeDefined();', | ||
'expect([1, 2, 3]).toEqual([1, 2, 3]);' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect().toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'No arguments passed to expect()' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect().toEqual("something");', | ||
errors: [ | ||
{ | ||
message: 'No arguments passed to expect()' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect("something", "else").toEqual("something");', | ||
errors: [ | ||
{ | ||
message: 'More than one argument passed to expect()' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect("something");', | ||
errors: [ | ||
{ | ||
message: 'Nothing called on expect()' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'expect();', | ||
errors: [ | ||
{ | ||
message: 'No arguments passed to expect()' | ||
}, | ||
{ | ||
message: 'Nothing called on expect()' | ||
} | ||
] | ||
} | ||
] | ||
}) |