Skip to content

Commit 0d77300

Browse files
feat(no-large-snapshots): add setting to define maxSize by snapshot type (#524)
* feat(no-large-snapshots): add setting to define maxSize by snapshot type * chore(no-large-snapshot): refactor to remove externalMaxSize option
1 parent 7730f75 commit 0d77300

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

docs/rules/no-large-snapshots.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,27 @@ line 4
9898

9999
## Options
100100

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

104104
In an `eslintrc` file:
105105

106106
```json
107107
...
108108
"rules": {
109-
"jest/no-large-snapshots": ["warn", { "maxSize": 12 }]
109+
"jest/no-large-snapshots": ["warn", { "maxSize": 12, "inlineMaxSize": 6 }]
110110
}
111111
...
112112
```
113113

114+
Max number of lines allowed could be defined by snapshot type (Inline and
115+
External). Use `inlineMaxSize` for
116+
[Inline Snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
117+
size and `maxSize` for
118+
[External Snapshots](https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest).
119+
If only `maxSize` is provided on options, the value of `maxSize` will be used to
120+
both snapshot types (Inline and External).
121+
114122
In addition there is an option for whitelisting large snapshot files. Since
115123
`//eslint` comments will be removed when a `.snap` file is updated, this option
116124
provides a way of whitelisting large snapshots. The list of whitelistedSnapshots

src/rules/__tests__/no-large-snapshots.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ ruleTester.run('no-large-snapshots', rule, {
4646
filename: 'mock.jsx',
4747
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
4848
},
49+
{
50+
filename: 'mock.jsx',
51+
code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'),
52+
options: [
53+
{
54+
maxSize: 19,
55+
inlineMaxSize: 21,
56+
},
57+
],
58+
},
59+
{
60+
filename: 'mock.jsx',
61+
code: generateExpectInlineSnapsCode(60, 'toMatchInlineSnapshot'),
62+
options: [
63+
{
64+
maxSize: 61,
65+
},
66+
],
67+
},
4968
{
5069
// "should not report if node has fewer lines of code than limit"
5170
filename: '/mock-component.jsx.snap',
@@ -63,6 +82,16 @@ ruleTester.run('no-large-snapshots', rule, {
6382
},
6483
],
6584
},
85+
{
86+
filename: '/mock-component.jsx.snap',
87+
code: generateExportsSnapshotString(20),
88+
options: [
89+
{
90+
maxSize: 21,
91+
inlineMaxSize: 19,
92+
},
93+
],
94+
},
6695
],
6796
invalid: [
6897
{
@@ -88,6 +117,20 @@ ruleTester.run('no-large-snapshots', rule, {
88117
},
89118
],
90119
},
120+
{
121+
filename: 'mock.js',
122+
code: generateExpectInlineSnapsCode(
123+
50,
124+
'toThrowErrorMatchingInlineSnapshot',
125+
),
126+
options: [{ maxSize: 51, inlineMaxSize: 50 }],
127+
errors: [
128+
{
129+
messageId: 'tooLongSnapshots',
130+
data: { lineLimit: 50, lineCount: 51 },
131+
},
132+
],
133+
},
91134
{
92135
// "should report if node has more than 50 lines of code, and no sizeThreshold option is passed"
93136
filename: '/mock-component.jsx.snap',
@@ -111,6 +154,17 @@ ruleTester.run('no-large-snapshots', rule, {
111154
},
112155
],
113156
},
157+
{
158+
filename: '/mock-component.jsx.snap',
159+
code: generateExportsSnapshotString(100),
160+
options: [{ maxSize: 70, inlineMaxSize: 101 }],
161+
errors: [
162+
{
163+
messageId: 'tooLongSnapshots',
164+
data: { lineLimit: 70, lineCount: 100 },
165+
},
166+
],
167+
},
114168
{
115169
// "should report if maxSize is zero"
116170
filename: '/mock-component.jsx.snap',

src/rules/no-large-snapshots.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313

1414
interface RuleOptions {
1515
maxSize?: number;
16+
inlineMaxSize?: number;
1617
whitelistedSnapshots?: Record<string, Array<string | RegExp>>;
1718
}
1819

@@ -92,6 +93,9 @@ export default createRule<[RuleOptions], MessageId>({
9293
maxSize: {
9394
type: 'number',
9495
},
96+
inlineMaxSize: {
97+
type: 'number',
98+
},
9599
whitelistedSnapshots: {
96100
type: 'object',
97101
patternProperties: {
@@ -125,7 +129,10 @@ export default createRule<[RuleOptions], MessageId>({
125129
'toThrowErrorMatchingInlineSnapshot',
126130
))
127131
) {
128-
reportOnViolation(context, node, options);
132+
reportOnViolation(context, node, {
133+
...options,
134+
maxSize: options.inlineMaxSize ?? options.maxSize,
135+
});
129136
}
130137
},
131138
};

0 commit comments

Comments
 (0)