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

fix(no-large-snapshots): only count size of template string for inline snapshots #1005

Merged
merged 1 commit into from Dec 27, 2021
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
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