Skip to content

Commit b5a13fd

Browse files
tryggvigySimenB
authored andcommittedJul 17, 2018
feat(rules): add prefer-inline-snapshots (#129)
1 parent 2b55a86 commit b5a13fd

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ for more information about extending configuration files.
9494
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
9595
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
9696
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
97+
| [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] |
9798
| [valid-describe][] | Enforce valid `describe()` callback | | |
9899
| [valid-expect-in-promise][] | Enforce having return statement when testing with promises | | |
99100
| [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | |
@@ -117,6 +118,7 @@ for more information about extending configuration files.
117118
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
118119
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
119120
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
121+
[prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md
120122
[valid-describe]: docs/rules/valid-describe.md
121123
[valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md
122124
[valid-expect]: docs/rules/valid-expect.md

‎docs/rules/prefer-inline-snapshots.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Suggest using inline snapshots (prefer-inline-snapshots)
2+
3+
In order to make snapshot tests more managable and reviewable
4+
`toMatchInlineSnapshot()` and `toThrowErrorMatchingInlineSnapshot` should be
5+
used to write the snapshots inline in the test file.
6+
7+
## Rule details
8+
9+
This rule triggers a warning if `toMatchSnapshot()` or
10+
`toThrowErrorMatchingSnapshot` is used to capture a snapshot.
11+
12+
The following pattern is considered warning:
13+
14+
```js
15+
expect(obj).toMatchSnapshot();
16+
```
17+
18+
```js
19+
expect(error).toThrowErrorMatchingSnapshot();
20+
```
21+
22+
The following pattern is not warning:
23+
24+
```js
25+
expect(obj).toMatchInlineSnapshot();
26+
```
27+
28+
```js
29+
expect(error).toThrowErrorMatchingInlineSnapshot();
30+
```

‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const validDescribe = require('./rules/valid-describe');
1717
const validExpect = require('./rules/valid-expect');
1818
const preferExpectAssertions = require('./rules/prefer-expect-assertions');
1919
const validExpectInPromise = require('./rules/valid-expect-in-promise');
20+
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
2021

2122
const snapshotProcessor = require('./processors/snapshot-processor');
2223

@@ -81,5 +82,6 @@ module.exports = {
8182
'valid-expect': validExpect,
8283
'prefer-expect-assertions': preferExpectAssertions,
8384
'valid-expect-in-promise': validExpectInPromise,
85+
'prefer-inline-snapshots': preferInlineSnapshots,
8486
},
8587
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../prefer-inline-snapshots');
5+
6+
const ruleTester = new RuleTester();
7+
8+
ruleTester.run('prefer-inline-snapshots', rule, {
9+
valid: [
10+
'expect(something).toMatchInlineSnapshot();',
11+
'expect(something).toThrowErrorMatchingInlineSnapshot();',
12+
],
13+
invalid: [
14+
{
15+
code: 'expect(something).toMatchSnapshot();',
16+
errors: [
17+
{
18+
message: 'Use toMatchInlineSnapshot() instead',
19+
column: 19,
20+
line: 1,
21+
},
22+
],
23+
output: 'expect(something).toMatchInlineSnapshot();',
24+
},
25+
{
26+
code: 'expect(something).toThrowErrorMatchingSnapshot();',
27+
errors: [
28+
{
29+
message: 'Use toThrowErrorMatchingInlineSnapshot() instead',
30+
column: 19,
31+
line: 1,
32+
},
33+
],
34+
output: 'expect(something).toThrowErrorMatchingInlineSnapshot();',
35+
},
36+
],
37+
});

‎rules/prefer-inline-snapshots.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const getDocsUrl = require('./util').getDocsUrl;
4+
5+
module.exports = {
6+
meta: {
7+
docs: {
8+
url: getDocsUrl(__filename),
9+
},
10+
fixable: 'code',
11+
},
12+
create(context) {
13+
return {
14+
CallExpression(node) {
15+
const propertyName = node.callee.property && node.callee.property.name;
16+
if (propertyName === 'toMatchSnapshot') {
17+
context.report({
18+
fix(fixer) {
19+
return [
20+
fixer.replaceText(
21+
node.callee.property,
22+
'toMatchInlineSnapshot'
23+
),
24+
];
25+
},
26+
message: 'Use toMatchInlineSnapshot() instead',
27+
node: node.callee.property,
28+
});
29+
} else if (propertyName === 'toThrowErrorMatchingSnapshot') {
30+
context.report({
31+
fix(fixer) {
32+
return [
33+
fixer.replaceText(
34+
node.callee.property,
35+
'toThrowErrorMatchingInlineSnapshot'
36+
),
37+
];
38+
},
39+
message: 'Use toThrowErrorMatchingInlineSnapshot() instead',
40+
node: node.callee.property,
41+
});
42+
}
43+
},
44+
};
45+
},
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.