Skip to content

Commit 5bea38f

Browse files
authored
fix(no-large-snapshots): only count size of template string for inline snapshots (#1005)
1 parent 7739638 commit 5bea38f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TSESLint } from '@typescript-eslint/experimental-utils';
2+
import dedent from 'dedent';
23
import rule from '../no-large-snapshots';
34
import { espreeParser } from './test-utils';
45

@@ -27,6 +28,7 @@ ruleTester.run('no-large-snapshots', rule, {
2728
'expect(something)',
2829
'expect(something).toBe(1)',
2930
'expect(something).toMatchInlineSnapshot',
31+
'expect(something).toMatchInlineSnapshot()',
3032
{
3133
filename: 'mock.js',
3234
code: generateExpectInlineSnapsCode(2, 'toMatchInlineSnapshot'),
@@ -57,6 +59,23 @@ ruleTester.run('no-large-snapshots', rule, {
5759
},
5860
],
5961
},
62+
{
63+
filename: 'mock.jsx',
64+
code: dedent`
65+
expect(
66+
functionUnderTest(
67+
arg1,
68+
arg2,
69+
arg3
70+
)
71+
).toMatchInlineSnapshot(${generateSnapshotLines(60)});
72+
`,
73+
options: [
74+
{
75+
maxSize: 61,
76+
},
77+
],
78+
},
6079
{
6180
// "should not report if node has fewer lines of code than limit"
6281
filename: '/mock-component.jsx.snap',

src/rules/no-large-snapshots.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type RuleContext = TSESLint.RuleContext<MessageId, [RuleOptions]>;
2424

2525
const reportOnViolation = (
2626
context: RuleContext,
27-
node: TSESTree.CallExpression | TSESTree.ExpressionStatement,
27+
node: TSESTree.CallExpressionArgument | TSESTree.ExpressionStatement,
2828
{ maxSize: lineLimit = 50, allowedSnapshots = {} }: RuleOptions,
2929
) => {
3030
const startLine = node.loc.start.line;
@@ -130,9 +130,10 @@ export default createRule<[RuleOptions], MessageId>({
130130
[
131131
'toMatchInlineSnapshot',
132132
'toThrowErrorMatchingInlineSnapshot',
133-
].includes(matcher.name)
133+
].includes(matcher.name) &&
134+
matcher.arguments?.length
134135
) {
135-
reportOnViolation(context, matcher.node.parent, {
136+
reportOnViolation(context, matcher.arguments[0], {
136137
...options,
137138
maxSize: options.inlineMaxSize ?? options.maxSize,
138139
});

0 commit comments

Comments
 (0)