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-called-with rule
Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` instead of the forms without argument checking. Similar to prefer-toHaveBeenCalledWith from eslint-plugin-jasmine. This rule has no opinion on which matcher alias should be used.
- Loading branch information
Showing
5 changed files
with
109 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,32 @@ | ||
# Suggest using `toBeCalledWith` OR `toHaveBeenCalledWith` (prefer-called-with) | ||
|
||
The `toBeCalled()` matcher is used to assert that a mock function has been | ||
called one or more times, without checking the arguments passed. The assertion | ||
is stronger when arguments are also validated using the `toBeCalledWith()` | ||
matcher. When some arguments are difficult to check, using generic match like | ||
`expect.anything()` at least enforces number and position of arguments. | ||
|
||
This rule warns if the form without argument checking is used, except for `.not` | ||
enforcing a function has never been called. | ||
|
||
## Rule details | ||
|
||
The following patterns are warnings: | ||
|
||
```js | ||
expect(someFunction).toBeCalled(); | ||
|
||
expect(someFunction).toHaveBeenCalled(); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
expect(noArgsFunction).toBeCalledWith(); | ||
|
||
expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date)); | ||
|
||
expect(anyArgsFunction).toBeCalledTimes(1); | ||
|
||
expect(uncalledFunction).not.toBeCalled(); | ||
``` |
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,44 @@ | ||
'use strict'; | ||
|
||
const { RuleTester } = require('eslint'); | ||
const rule = require('../prefer-called-with'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer-called-with', rule, { | ||
valid: [ | ||
'expect(fn).toBeCalledWith();', | ||
'expect(fn).toHaveBeenCalledWith();', | ||
'expect(fn).toBeCalledWith(expect.anything());', | ||
'expect(fn).toHaveBeenCalledWith(expect.anything());', | ||
'expect(fn).not.toBeCalled();', | ||
'expect(fn).not.toHaveBeenCalled();', | ||
'expect(fn).not.toBeCalledWith();', | ||
'expect(fn).not.toHaveBeenCalledWith();', | ||
'expect(fn).toBeCalledTimes(0);', | ||
'expect(fn).toHaveBeenCalledTimes(0);', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(fn).toBeCalled();', | ||
errors: [ | ||
{ | ||
message: 'Prefer toBeCalledWith(/* expected args */)', | ||
column: 12, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(fn).toHaveBeenCalled();', | ||
errors: [ | ||
{ | ||
message: 'Prefer toHaveBeenCalledWith(/* expected args */)', | ||
column: 12, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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,29 @@ | ||
'use strict'; | ||
|
||
const { getDocsUrl, expectCase, expectNotCase, method } = require('./util'); | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
// Could check resolves/rejects here but not a likely idiom. | ||
if (expectCase(node) && !expectNotCase(node)) { | ||
const methodNode = method(node); | ||
const { name } = methodNode; | ||
if (name === 'toBeCalled' || name === 'toHaveBeenCalled') { | ||
context.report({ | ||
data: { name }, | ||
message: 'Prefer {{name}}With(/* expected args */)', | ||
node: methodNode, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |