Skip to content

Commit

Permalink
chore(no-large-snapshot): refactor to remove externalMaxSize option
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardovillela committed Feb 12, 2020
1 parent 00fd6a3 commit 1b8bcd3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 76 deletions.
20 changes: 6 additions & 14 deletions docs/rules/no-large-snapshots.md
Expand Up @@ -98,34 +98,26 @@ 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:

```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
Expand Down
17 changes: 13 additions & 4 deletions src/rules/__tests__/no-large-snapshots.test.ts
Expand Up @@ -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',
Expand All @@ -78,7 +87,7 @@ ruleTester.run('no-large-snapshots', rule, {
code: generateExportsSnapshotString(20),
options: [
{
externalMaxSize: 21,
maxSize: 21,
inlineMaxSize: 19,
},
],
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
76 changes: 18 additions & 58 deletions src/rules/no-large-snapshots.ts
Expand Up @@ -14,7 +14,6 @@ import {
interface RuleOptions {
maxSize?: number;
inlineMaxSize?: number;
externalMaxSize?: number;
whitelistedSnapshots?: Record<string, Array<string | RegExp>>;
}

Expand All @@ -25,10 +24,7 @@ type RuleContext = TSESLint.RuleContext<MessageId, [RuleOptions]>;
const reportOnViolation = (
context: RuleContext,
node: TSESTree.CallExpression | TSESTree.ExpressionStatement,
{
maxSize: lineLimit = 50,
whitelistedSnapshots = {},
}: Omit<RuleOptions, 'inlineMaxSize' | 'externalMaxSize'>,
{ maxSize: lineLimit = 50, whitelistedSnapshots = {} }: RuleOptions,
) => {
const startLine = node.loc.start.line;
const endLine = node.loc.end.line;
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
},
],
},
Expand All @@ -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')) {
Expand All @@ -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,
});
}
},
};
Expand Down

0 comments on commit 1b8bcd3

Please sign in to comment.