File tree 5 files changed +117
-0
lines changed
5 files changed +117
-0
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ for more information about extending configuration files.
94
94
| [ prefer-to-be-null] [ ] | Suggest using ` toBeNull() ` | | ![ fixable-green] [ ] |
95
95
| [ prefer-to-be-undefined] [ ] | Suggest using ` toBeUndefined() ` | | ![ fixable-green] [ ] |
96
96
| [ prefer-to-have-length] [ ] | Suggest using ` toHaveLength() ` | ![ recommended] [ ] | ![ fixable-green] [ ] |
97
+ | [ prefer-inline-snapshots] [ ] | Suggest using ` toMatchInlineSnapshot() ` | | ![ fixable-green] [ ] |
97
98
| [ valid-describe] [ ] | Enforce valid ` describe() ` callback | | |
98
99
| [ valid-expect-in-promise] [ ] | Enforce having return statement when testing with promises | | |
99
100
| [ valid-expect] [ ] | Enforce valid ` expect() ` usage | ![ recommended] [ ] | |
@@ -117,6 +118,7 @@ for more information about extending configuration files.
117
118
[ prefer-to-be-null ] : docs/rules/prefer-to-be-null.md
118
119
[ prefer-to-be-undefined ] : docs/rules/prefer-to-be-undefined.md
119
120
[ prefer-to-have-length ] : docs/rules/prefer-to-have-length.md
121
+ [ prefer-inline-snapshots ] : docs/rules/prefer-inline-snapshots.md
120
122
[ valid-describe ] : docs/rules/valid-describe.md
121
123
[ valid-expect-in-promise ] : docs/rules/valid-expect-in-promise.md
122
124
[ valid-expect ] : docs/rules/valid-expect.md
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ const validDescribe = require('./rules/valid-describe');
17
17
const validExpect = require ( './rules/valid-expect' ) ;
18
18
const preferExpectAssertions = require ( './rules/prefer-expect-assertions' ) ;
19
19
const validExpectInPromise = require ( './rules/valid-expect-in-promise' ) ;
20
+ const preferInlineSnapshots = require ( './rules/prefer-inline-snapshots' ) ;
20
21
21
22
const snapshotProcessor = require ( './processors/snapshot-processor' ) ;
22
23
@@ -81,5 +82,6 @@ module.exports = {
81
82
'valid-expect' : validExpect ,
82
83
'prefer-expect-assertions' : preferExpectAssertions ,
83
84
'valid-expect-in-promise' : validExpectInPromise ,
85
+ 'prefer-inline-snapshots' : preferInlineSnapshots ,
84
86
} ,
85
87
} ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments