-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
223 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,41 @@ | ||
# Suggest using `jest.spyOn()` (prefer-spy-on) | ||
|
||
When mocking a function by overwriting a property you have to manually restore | ||
the original implementation when cleaning up. When using `jest.spyOn()` Jest | ||
keeps track of changes, and they can be restored with `jest.restoreAllMocks()`, | ||
`mockFn.mockRestore()` or by setting `restoreMocks` to `true` in the Jest | ||
config. | ||
|
||
Note: The mock created by `jest.spyOn()` still behaves the same as the original | ||
function. The original function can be overwritten with | ||
`mockFn.mockImplementation()` or by some of the | ||
[other mock functions](https://jestjs.io/docs/en/mock-function-api). | ||
|
||
```js | ||
Date.now = jest.fn(); // Original behaviour lost, returns undefined | ||
|
||
jest.spyOn(Date, 'now'); // Turned into a mock function but behaviour hasn't changed | ||
jest.spyOn(Date, 'now').mockImplementation(() => 10); // Will always return 10 | ||
jest.spyOn(Date, 'now').mockReturnValue(10); // Will always return 10 | ||
``` | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if an object's property is overwritten with a jest | ||
mock. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
Date.now = jest.fn(); | ||
Date.now = jest.fn(() => 10); | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
jest.spyOn(Date, 'now'); | ||
jest.spyOn(Date, 'now').mockImplementation(() => 10); | ||
``` |
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,108 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../prefer-spy-on'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-spy-on', rule, { | ||
valid: [ | ||
'Date.now = () => 10', | ||
'window.fetch = jest.fn', | ||
'obj.mock = jest.something()', | ||
'const mock = jest.fn()', | ||
'mock = jest.fn()', | ||
'const mockObj = { mock: jest.fn() }', | ||
'mockObj = { mock: jest.fn() }', | ||
'window[`${name}`] = jest[`fn${expression}`]()', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'obj.a = jest.fn(); const test = 10;', | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: "jest.spyOn(obj, 'a'); const test = 10;", | ||
}, | ||
{ | ||
code: "Date['now'] = jest['fn']()", | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: "jest.spyOn(Date, 'now')", | ||
}, | ||
{ | ||
code: 'window[`${name}`] = jest[`fn`]()', | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: 'jest.spyOn(window, `${name}`)', | ||
}, | ||
{ | ||
code: "obj['prop' + 1] = jest['fn']()", | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: "jest.spyOn(obj, 'prop' + 1)", | ||
}, | ||
{ | ||
code: 'obj.one.two = jest.fn(); const test = 10;', | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: "jest.spyOn(obj.one, 'two'); const test = 10;", | ||
}, | ||
{ | ||
code: 'obj.a = jest.fn(() => 10)', | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: "jest.spyOn(obj, 'a').mockImplementation(() => 10)", | ||
}, | ||
{ | ||
code: | ||
"obj.a.b = jest.fn(() => ({})).mockReturnValue('default').mockReturnValueOnce('first call'); test();", | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: | ||
"jest.spyOn(obj.a, 'b').mockImplementation(() => ({})).mockReturnValue('default').mockReturnValueOnce('first call'); test();", | ||
}, | ||
{ | ||
code: 'window.fetch = jest.fn(() => ({})).one.two().three().four', | ||
errors: [ | ||
{ | ||
message: 'Use jest.spyOn() instead.', | ||
type: 'AssignmentExpression', | ||
}, | ||
], | ||
output: | ||
"jest.spyOn(window, 'fetch').mockImplementation(() => ({})).one.two().three().four", | ||
}, | ||
], | ||
}); |
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,67 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
const getNodeName = require('./util').getNodeName; | ||
|
||
const getJestFnCall = node => { | ||
if (node.type !== 'CallExpression' && node.type !== 'MemberExpression') { | ||
return null; | ||
} | ||
|
||
const obj = node.callee ? node.callee.object : node.object; | ||
|
||
if (obj.type === 'Identifier') { | ||
return node.type === 'CallExpression' && | ||
getNodeName(node.callee) === 'jest.fn' | ||
? node | ||
: null; | ||
} | ||
|
||
return getJestFnCall(obj); | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
return { | ||
AssignmentExpression(node) { | ||
if (node.left.type !== 'MemberExpression') return; | ||
|
||
const jestFnCall = getJestFnCall(node.right); | ||
|
||
if (!jestFnCall) return; | ||
|
||
context.report({ | ||
node, | ||
message: 'Use jest.spyOn() instead.', | ||
fix(fixer) { | ||
const leftPropQuote = | ||
node.left.property.type === 'Identifier' ? "'" : ''; | ||
const arg = jestFnCall.arguments[0]; | ||
const argSource = arg && context.getSourceCode().getText(arg); | ||
const mockImplementation = argSource | ||
? `.mockImplementation(${argSource})` | ||
: ''; | ||
|
||
return [ | ||
fixer.insertTextBefore(node.left, `jest.spyOn(`), | ||
fixer.replaceTextRange( | ||
[node.left.object.end, node.left.property.start], | ||
`, ${leftPropQuote}` | ||
), | ||
fixer.replaceTextRange( | ||
[node.left.property.end, jestFnCall.end], | ||
`${leftPropQuote})${mockImplementation}` | ||
), | ||
]; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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