From 0d77300e61adc7a5aa84f34ff4ccc164075d5f41 Mon Sep 17 00:00:00 2001 From: Leonardo Villela Date: Sun, 23 Feb 2020 04:00:54 +0000 Subject: [PATCH] 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 --- docs/rules/no-large-snapshots.md | 12 ++++- .../__tests__/no-large-snapshots.test.ts | 54 +++++++++++++++++++ src/rules/no-large-snapshots.ts | 9 +++- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-large-snapshots.md b/docs/rules/no-large-snapshots.md index f785193a9..f9a402bfb 100644 --- a/docs/rules/no-large-snapshots.md +++ b/docs/rules/no-large-snapshots.md @@ -98,7 +98,7 @@ line 4 ## Options -This rule has an option for modifying the max number of lines allowed for a +This rule has options for modifying the max number of lines allowed for a snapshot: In an `eslintrc` file: @@ -106,11 +106,19 @@ In an `eslintrc` file: ```json ... "rules": { - "jest/no-large-snapshots": ["warn", { "maxSize": 12 }] + "jest/no-large-snapshots": ["warn", { "maxSize": 12, "inlineMaxSize": 6 }] } ... ``` +Max number of lines allowed could be defined by snapshot type (Inline and +External). Use `inlineMaxSize` for +[Inline Snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots) +size and `maxSize` for +[External Snapshots](https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest). +If only `maxSize` is provided on options, the value of `maxSize` will be used to +both snapshot types (Inline and External). + In addition there is an option for whitelisting large snapshot files. Since `//eslint` comments will be removed when a `.snap` file is updated, this option provides a way of whitelisting large snapshots. The list of whitelistedSnapshots diff --git a/src/rules/__tests__/no-large-snapshots.test.ts b/src/rules/__tests__/no-large-snapshots.test.ts index 9c806e8eb..4bb1e538c 100644 --- a/src/rules/__tests__/no-large-snapshots.test.ts +++ b/src/rules/__tests__/no-large-snapshots.test.ts @@ -46,6 +46,25 @@ ruleTester.run('no-large-snapshots', rule, { filename: 'mock.jsx', code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'), }, + { + filename: 'mock.jsx', + code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'), + options: [ + { + maxSize: 19, + inlineMaxSize: 21, + }, + ], + }, + { + filename: 'mock.jsx', + code: generateExpectInlineSnapsCode(60, 'toMatchInlineSnapshot'), + options: [ + { + maxSize: 61, + }, + ], + }, { // "should not report if node has fewer lines of code than limit" filename: '/mock-component.jsx.snap', @@ -63,6 +82,16 @@ ruleTester.run('no-large-snapshots', rule, { }, ], }, + { + filename: '/mock-component.jsx.snap', + code: generateExportsSnapshotString(20), + options: [ + { + maxSize: 21, + inlineMaxSize: 19, + }, + ], + }, ], invalid: [ { @@ -88,6 +117,20 @@ ruleTester.run('no-large-snapshots', rule, { }, ], }, + { + filename: 'mock.js', + code: generateExpectInlineSnapsCode( + 50, + 'toThrowErrorMatchingInlineSnapshot', + ), + options: [{ maxSize: 51, inlineMaxSize: 50 }], + errors: [ + { + messageId: 'tooLongSnapshots', + data: { lineLimit: 50, lineCount: 51 }, + }, + ], + }, { // "should report if node has more than 50 lines of code, and no sizeThreshold option is passed" filename: '/mock-component.jsx.snap', @@ -111,6 +154,17 @@ ruleTester.run('no-large-snapshots', rule, { }, ], }, + { + filename: '/mock-component.jsx.snap', + code: generateExportsSnapshotString(100), + options: [{ maxSize: 70, inlineMaxSize: 101 }], + errors: [ + { + messageId: 'tooLongSnapshots', + data: { lineLimit: 70, lineCount: 100 }, + }, + ], + }, { // "should report if maxSize is zero" filename: '/mock-component.jsx.snap', diff --git a/src/rules/no-large-snapshots.ts b/src/rules/no-large-snapshots.ts index 4d3c4a9ec..d4f364b1f 100644 --- a/src/rules/no-large-snapshots.ts +++ b/src/rules/no-large-snapshots.ts @@ -13,6 +13,7 @@ import { interface RuleOptions { maxSize?: number; + inlineMaxSize?: number; whitelistedSnapshots?: Record>; } @@ -92,6 +93,9 @@ export default createRule<[RuleOptions], MessageId>({ maxSize: { type: 'number', }, + inlineMaxSize: { + type: 'number', + }, whitelistedSnapshots: { type: 'object', patternProperties: { @@ -125,7 +129,10 @@ export default createRule<[RuleOptions], MessageId>({ 'toThrowErrorMatchingInlineSnapshot', )) ) { - reportOnViolation(context, node, options); + reportOnViolation(context, node, { + ...options, + maxSize: options.inlineMaxSize ?? options.maxSize, + }); } }, };