Skip to content

Commit

Permalink
feat(no-large-snapshots): option for whitelisting snapshots (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmonro authored and SimenB committed Jul 16, 2019
1 parent e292cb8 commit 25e3970
Show file tree
Hide file tree
Showing 6 changed files with 855 additions and 10 deletions.
48 changes: 47 additions & 1 deletion docs/rules/no-large-snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ line 4

## Options

This rule has option for modifying the max number of lines allowed for a
This rule has an option for modifying the max number of lines allowed for a
snapshot:

In an `eslintrc` file:
Expand All @@ -110,3 +110,49 @@ In an `eslintrc` file:
}
...
```

In addition there is an option for whitelisting large snapshot files. Since
`//eslint` comments will be removed when a `.snap` file is updated, this option
provides a way of whitelisting large snapshots. The list of whitelistedSnapshots
is keyed first on the absolute filepath of the snapshot file. You can then
provide an array of strings to match the snapshot names against. If you're using
a `.eslintrc.js` file, you can use regular expressions AND strings.

In an `.eslintrc.js` file:

```javascript
...

"rules": {
"jest/no-large-snapshots": ["error",
{
"whitelistedSnapshots": {
"/path/to/file.js.snap": ["snapshot name 1", /a big snapshot \d+/]
}
}]
}

...
```

Note: If you store your paths as relative paths, you can use `path.resolve` so
that it can be shared between computers. For example, suppose you have your
whitelisted snapshots in a file called `allowed-snaps.js` which stores them as
relative paths. To convert them to absolute paths you can do something like the
following:

```javascript
const path = require('path');
const {mapKeys} = require('lodash');


const allowedSnapshots = require('./allowed-snaps.js');
const whitelistedSnapshots = mapKeys(allowedSnapshots, (val, file) => path.resolve(__dirname, file));

...
rules: {
"jest/no-large-snapshots": ["error",
{ whitelistedSnapshots }
]
}
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@babel/preset-env": "^7.4.4",
"@commitlint/cli": "^6.0.0",
"@commitlint/config-conventional": "^6.0.0",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"eslint": "^5.1.0",
"eslint-config-prettier": "^5.1.0",
Expand Down
Loading

0 comments on commit 25e3970

Please sign in to comment.