Skip to content

Commit

Permalink
fix(no-large-snapshots): only count size of template string for inlin…
Browse files Browse the repository at this point in the history
…e snapshots (#1005)
  • Loading branch information
G-Rath committed Dec 27, 2021
1 parent 7739638 commit 5bea38f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/rules/__tests__/no-large-snapshots.test.ts
@@ -1,4 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import dedent from 'dedent';
import rule from '../no-large-snapshots';
import { espreeParser } from './test-utils';

Expand Down Expand Up @@ -27,6 +28,7 @@ ruleTester.run('no-large-snapshots', rule, {
'expect(something)',
'expect(something).toBe(1)',
'expect(something).toMatchInlineSnapshot',
'expect(something).toMatchInlineSnapshot()',
{
filename: 'mock.js',
code: generateExpectInlineSnapsCode(2, 'toMatchInlineSnapshot'),
Expand Down Expand Up @@ -57,6 +59,23 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: 'mock.jsx',
code: dedent`
expect(
functionUnderTest(
arg1,
arg2,
arg3
)
).toMatchInlineSnapshot(${generateSnapshotLines(60)});
`,
options: [
{
maxSize: 61,
},
],
},
{
// "should not report if node has fewer lines of code than limit"
filename: '/mock-component.jsx.snap',
Expand Down
7 changes: 4 additions & 3 deletions src/rules/no-large-snapshots.ts
Expand Up @@ -24,7 +24,7 @@ type RuleContext = TSESLint.RuleContext<MessageId, [RuleOptions]>;

const reportOnViolation = (
context: RuleContext,
node: TSESTree.CallExpression | TSESTree.ExpressionStatement,
node: TSESTree.CallExpressionArgument | TSESTree.ExpressionStatement,
{ maxSize: lineLimit = 50, allowedSnapshots = {} }: RuleOptions,
) => {
const startLine = node.loc.start.line;
Expand Down Expand Up @@ -130,9 +130,10 @@ export default createRule<[RuleOptions], MessageId>({
[
'toMatchInlineSnapshot',
'toThrowErrorMatchingInlineSnapshot',
].includes(matcher.name)
].includes(matcher.name) &&
matcher.arguments?.length
) {
reportOnViolation(context, matcher.node.parent, {
reportOnViolation(context, matcher.arguments[0], {
...options,
maxSize: options.inlineMaxSize ?? options.maxSize,
});
Expand Down

0 comments on commit 5bea38f

Please sign in to comment.