From 5bea38f9773ab686f08a7cc25247a782d50aa5ed Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 28 Dec 2021 10:35:12 +1300 Subject: [PATCH] fix(no-large-snapshots): only count size of template string for inline snapshots (#1005) --- .../__tests__/no-large-snapshots.test.ts | 19 +++++++++++++++++++ src/rules/no-large-snapshots.ts | 7 ++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/rules/__tests__/no-large-snapshots.test.ts b/src/rules/__tests__/no-large-snapshots.test.ts index 809e00b14..7fbd29f09 100644 --- a/src/rules/__tests__/no-large-snapshots.test.ts +++ b/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'; @@ -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'), @@ -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', diff --git a/src/rules/no-large-snapshots.ts b/src/rules/no-large-snapshots.ts index e7fae9ade..d0687dec4 100644 --- a/src/rules/no-large-snapshots.ts +++ b/src/rules/no-large-snapshots.ts @@ -24,7 +24,7 @@ type RuleContext = TSESLint.RuleContext; 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; @@ -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, });