Skip to content

Commit 25e3970

Browse files
benmonroSimenB
authored andcommitted
feat(no-large-snapshots): option for whitelisting snapshots (#288)
1 parent e292cb8 commit 25e3970

File tree

6 files changed

+855
-10
lines changed

6 files changed

+855
-10
lines changed

docs/rules/no-large-snapshots.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ line 4
9898

9999
## Options
100100

101-
This rule has option for modifying the max number of lines allowed for a
101+
This rule has an option for modifying the max number of lines allowed for a
102102
snapshot:
103103

104104
In an `eslintrc` file:
@@ -110,3 +110,49 @@ In an `eslintrc` file:
110110
}
111111
...
112112
```
113+
114+
In addition there is an option for whitelisting large snapshot files. Since
115+
`//eslint` comments will be removed when a `.snap` file is updated, this option
116+
provides a way of whitelisting large snapshots. The list of whitelistedSnapshots
117+
is keyed first on the absolute filepath of the snapshot file. You can then
118+
provide an array of strings to match the snapshot names against. If you're using
119+
a `.eslintrc.js` file, you can use regular expressions AND strings.
120+
121+
In an `.eslintrc.js` file:
122+
123+
```javascript
124+
...
125+
126+
"rules": {
127+
"jest/no-large-snapshots": ["error",
128+
{
129+
"whitelistedSnapshots": {
130+
"/path/to/file.js.snap": ["snapshot name 1", /a big snapshot \d+/]
131+
}
132+
}]
133+
}
134+
135+
...
136+
```
137+
138+
Note: If you store your paths as relative paths, you can use `path.resolve` so
139+
that it can be shared between computers. For example, suppose you have your
140+
whitelisted snapshots in a file called `allowed-snaps.js` which stores them as
141+
relative paths. To convert them to absolute paths you can do something like the
142+
following:
143+
144+
```javascript
145+
const path = require('path');
146+
const {mapKeys} = require('lodash');
147+
148+
149+
const allowedSnapshots = require('./allowed-snaps.js');
150+
const whitelistedSnapshots = mapKeys(allowedSnapshots, (val, file) => path.resolve(__dirname, file));
151+
152+
...
153+
rules: {
154+
"jest/no-large-snapshots": ["error",
155+
{ whitelistedSnapshots }
156+
]
157+
}
158+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@babel/preset-env": "^7.4.4",
4141
"@commitlint/cli": "^6.0.0",
4242
"@commitlint/config-conventional": "^6.0.0",
43+
"babel-eslint": "^10.0.2",
4344
"babel-jest": "^24.8.0",
4445
"eslint": "^5.1.0",
4546
"eslint-config-prettier": "^5.1.0",

0 commit comments

Comments
 (0)