Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(no-large-snapshots): add setting to define maxSize by snapshot type #524

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/rules/no-large-snapshots.md
Expand Up @@ -98,19 +98,27 @@ line 4

## Options

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

In an `eslintrc` file:

```json
...
"rules": {
"jest/no-large-snapshots": ["warn", { "maxSize": 12 }]
"jest/no-large-snapshots": ["warn", { "maxSize": 12, "inlineMaxSize": 6 }]
}
...
```

Max number of lines allowed could be defined by snapshot type (Inline and
External). Use `inlineMaxSize` for
[Inline Snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
size and `maxSize` for
[External Snapshots](https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest).
If only `maxSize` is provided on options, the value of `maxSize` will be used to
both snapshot types (Inline and External).

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
Expand Down
54 changes: 54 additions & 0 deletions src/rules/__tests__/no-large-snapshots.test.ts
Expand Up @@ -46,6 +46,25 @@ ruleTester.run('no-large-snapshots', rule, {
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
},
{
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'),
options: [
{
maxSize: 19,
inlineMaxSize: 21,
},
],
},
{
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(60, 'toMatchInlineSnapshot'),
options: [
{
maxSize: 61,
},
],
},
{
// "should not report if node has fewer lines of code than limit"
filename: '/mock-component.jsx.snap',
Expand All @@ -63,6 +82,16 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: '/mock-component.jsx.snap',
code: generateExportsSnapshotString(20),
options: [
{
maxSize: 21,
inlineMaxSize: 19,
},
],
},
],
invalid: [
{
Expand All @@ -88,6 +117,20 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: 'mock.js',
code: generateExpectInlineSnapsCode(
50,
'toThrowErrorMatchingInlineSnapshot',
),
options: [{ maxSize: 51, inlineMaxSize: 50 }],
errors: [
{
messageId: 'tooLongSnapshots',
data: { lineLimit: 50, lineCount: 51 },
},
],
},
{
// "should report if node has more than 50 lines of code, and no sizeThreshold option is passed"
filename: '/mock-component.jsx.snap',
Expand All @@ -111,6 +154,17 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: '/mock-component.jsx.snap',
code: generateExportsSnapshotString(100),
options: [{ maxSize: 70, inlineMaxSize: 101 }],
errors: [
{
messageId: 'tooLongSnapshots',
data: { lineLimit: 70, lineCount: 100 },
},
],
},
{
// "should report if maxSize is zero"
filename: '/mock-component.jsx.snap',
Expand Down
9 changes: 8 additions & 1 deletion src/rules/no-large-snapshots.ts
Expand Up @@ -13,6 +13,7 @@ import {

interface RuleOptions {
maxSize?: number;
inlineMaxSize?: number;
whitelistedSnapshots?: Record<string, Array<string | RegExp>>;
}

Expand Down Expand Up @@ -92,6 +93,9 @@ export default createRule<[RuleOptions], MessageId>({
maxSize: {
type: 'number',
},
inlineMaxSize: {
type: 'number',
},
whitelistedSnapshots: {
type: 'object',
patternProperties: {
Expand Down Expand Up @@ -125,7 +129,10 @@ export default createRule<[RuleOptions], MessageId>({
'toThrowErrorMatchingInlineSnapshot',
))
) {
reportOnViolation(context, node, options);
reportOnViolation(context, node, {
...options,
maxSize: options.inlineMaxSize ?? options.maxSize,
});
}
},
};
Expand Down