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-inline-snapshots (#129)
- Loading branch information
Showing
5 changed files
with
117 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,30 @@ | ||
# Suggest using inline snapshots (prefer-inline-snapshots) | ||
|
||
In order to make snapshot tests more managable and reviewable | ||
`toMatchInlineSnapshot()` and `toThrowErrorMatchingInlineSnapshot` should be | ||
used to write the snapshots inline in the test file. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toMatchSnapshot()` or | ||
`toThrowErrorMatchingSnapshot` is used to capture a snapshot. | ||
|
||
The following pattern is considered warning: | ||
|
||
```js | ||
expect(obj).toMatchSnapshot(); | ||
``` | ||
|
||
```js | ||
expect(error).toThrowErrorMatchingSnapshot(); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(obj).toMatchInlineSnapshot(); | ||
``` | ||
|
||
```js | ||
expect(error).toThrowErrorMatchingInlineSnapshot(); | ||
``` |
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,37 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../prefer-inline-snapshots'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer-inline-snapshots', rule, { | ||
valid: [ | ||
'expect(something).toMatchInlineSnapshot();', | ||
'expect(something).toThrowErrorMatchingInlineSnapshot();', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(something).toMatchSnapshot();', | ||
errors: [ | ||
{ | ||
message: 'Use toMatchInlineSnapshot() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(something).toMatchInlineSnapshot();', | ||
}, | ||
{ | ||
code: 'expect(something).toThrowErrorMatchingSnapshot();', | ||
errors: [ | ||
{ | ||
message: 'Use toThrowErrorMatchingInlineSnapshot() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(something).toThrowErrorMatchingInlineSnapshot();', | ||
}, | ||
], | ||
}); |
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,46 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const propertyName = node.callee.property && node.callee.property.name; | ||
if (propertyName === 'toMatchSnapshot') { | ||
context.report({ | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceText( | ||
node.callee.property, | ||
'toMatchInlineSnapshot' | ||
), | ||
]; | ||
}, | ||
message: 'Use toMatchInlineSnapshot() instead', | ||
node: node.callee.property, | ||
}); | ||
} else if (propertyName === 'toThrowErrorMatchingSnapshot') { | ||
context.report({ | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceText( | ||
node.callee.property, | ||
'toThrowErrorMatchingInlineSnapshot' | ||
), | ||
]; | ||
}, | ||
message: 'Use toThrowErrorMatchingInlineSnapshot() instead', | ||
node: node.callee.property, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |