From 1b8bcd35b5476a13efada874cfbb41eb528e9b8d Mon Sep 17 00:00:00 2001 From: Leonardo Villela Date: Wed, 12 Feb 2020 02:13:16 +0000 Subject: [PATCH] chore(no-large-snapshot): refactor to remove externalMaxSize option --- docs/rules/no-large-snapshots.md | 20 ++--- .../__tests__/no-large-snapshots.test.ts | 17 ++++- src/rules/no-large-snapshots.ts | 76 +++++-------------- 3 files changed, 37 insertions(+), 76 deletions(-) diff --git a/docs/rules/no-large-snapshots.md b/docs/rules/no-large-snapshots.md index fe8090fee..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,26 +106,18 @@ In an `eslintrc` file: ```json ... "rules": { - "jest/no-large-snapshots": ["warn", { "maxSize": 12 }] + "jest/no-large-snapshots": ["warn", { "maxSize": 12, "inlineMaxSize": 6 }] } ... ``` -Also, max number of lines allowed could be defined by snapshot type (Inline and +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 `externalMaxSize` for +size and `maxSize` for [External Snapshots](https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest). - -In an `eslintrc` file: - -```json -... - "rules": { - "jest/no-large-snapshots": ["warn", { "inlineMaxSize": 20, "externalMaxSize": 40 }] - } -... -``` +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 diff --git a/src/rules/__tests__/no-large-snapshots.test.ts b/src/rules/__tests__/no-large-snapshots.test.ts index db6d1b381..4bb1e538c 100644 --- a/src/rules/__tests__/no-large-snapshots.test.ts +++ b/src/rules/__tests__/no-large-snapshots.test.ts @@ -51,11 +51,20 @@ ruleTester.run('no-large-snapshots', rule, { code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'), options: [ { - externalMaxSize: 19, + 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', @@ -78,7 +87,7 @@ ruleTester.run('no-large-snapshots', rule, { code: generateExportsSnapshotString(20), options: [ { - externalMaxSize: 21, + maxSize: 21, inlineMaxSize: 19, }, ], @@ -114,7 +123,7 @@ ruleTester.run('no-large-snapshots', rule, { 50, 'toThrowErrorMatchingInlineSnapshot', ), - options: [{ externalMaxSize: 51, inlineMaxSize: 50 }], + options: [{ maxSize: 51, inlineMaxSize: 50 }], errors: [ { messageId: 'tooLongSnapshots', @@ -148,7 +157,7 @@ ruleTester.run('no-large-snapshots', rule, { { filename: '/mock-component.jsx.snap', code: generateExportsSnapshotString(100), - options: [{ externalMaxSize: 70, inlineMaxSize: 101 }], + options: [{ maxSize: 70, inlineMaxSize: 101 }], errors: [ { messageId: 'tooLongSnapshots', diff --git a/src/rules/no-large-snapshots.ts b/src/rules/no-large-snapshots.ts index e855d93f2..d4f364b1f 100644 --- a/src/rules/no-large-snapshots.ts +++ b/src/rules/no-large-snapshots.ts @@ -14,7 +14,6 @@ import { interface RuleOptions { maxSize?: number; inlineMaxSize?: number; - externalMaxSize?: number; whitelistedSnapshots?: Record>; } @@ -25,10 +24,7 @@ type RuleContext = TSESLint.RuleContext; const reportOnViolation = ( context: RuleContext, node: TSESTree.CallExpression | TSESTree.ExpressionStatement, - { - maxSize: lineLimit = 50, - whitelistedSnapshots = {}, - }: Omit, + { maxSize: lineLimit = 50, whitelistedSnapshots = {} }: RuleOptions, ) => { const startLine = node.loc.start.line; const endLine = node.loc.end.line; @@ -76,14 +72,6 @@ const reportOnViolation = ( } }; -const buildOptionsByMaxSizeAttributeName = ( - options: RuleOptions, - attributeName: 'inlineMaxSize' | 'externalMaxSize', -) => ({ - ...options, - maxSize: options[attributeName] || options.maxSize, -}); - export default createRule<[RuleOptions], MessageId>({ name: __filename, meta: { @@ -100,45 +88,22 @@ export default createRule<[RuleOptions], MessageId>({ type: 'suggestion', schema: [ { - anyOf: [ - { - oneOf: [ - { - type: 'object', - properties: { - maxSize: { - type: 'number', - }, - }, - additionalProperties: false, - }, - { - type: 'object', - properties: { - externalMaxSize: { - type: 'number', - }, - inlineMaxSize: { - type: 'number', - }, - }, - additionalProperties: false, - }, - ], + type: 'object', + properties: { + maxSize: { + type: 'number', }, - { + inlineMaxSize: { + type: 'number', + }, + whitelistedSnapshots: { type: 'object', - properties: { - whitelistedSnapshots: { - type: 'object', - patternProperties: { - '.*': { type: 'array' }, - }, - }, + patternProperties: { + '.*': { type: 'array' }, }, - additionalProperties: false, }, - ], + }, + additionalProperties: false, }, ], }, @@ -147,11 +112,7 @@ export default createRule<[RuleOptions], MessageId>({ if (context.getFilename().endsWith('.snap')) { return { ExpressionStatement(node) { - reportOnViolation( - context, - node, - buildOptionsByMaxSizeAttributeName(options, 'externalMaxSize'), - ); + reportOnViolation(context, node, options); }, }; } else if (context.getFilename().endsWith('.js')) { @@ -168,11 +129,10 @@ export default createRule<[RuleOptions], MessageId>({ 'toThrowErrorMatchingInlineSnapshot', )) ) { - reportOnViolation( - context, - node, - buildOptionsByMaxSizeAttributeName(options, 'inlineMaxSize'), - ); + reportOnViolation(context, node, { + ...options, + maxSize: options.inlineMaxSize ?? options.maxSize, + }); } }, };