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

feat(no-large-snapshots): add setting to define maxSize by snapshot type #524

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions docs/rules/no-large-snapshots.md
Expand Up @@ -111,6 +111,22 @@ In an `eslintrc` file:
...
```

Also, max number of lines allowed could be defined by snapshot type (Inline and
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
External). Use `inlineMaxSize` for
[Inline Snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
size and `externalMaxSize` 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 }]
}
...
```

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
Expand Down
45 changes: 45 additions & 0 deletions src/rules/__tests__/no-large-snapshots.test.ts
Expand Up @@ -46,6 +46,16 @@ ruleTester.run('no-large-snapshots', rule, {
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
},
{
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'),
options: [
{
externalMaxSize: 19,
inlineMaxSize: 21,
},
],
},
{
// "should not report if node has fewer lines of code than limit"
filename: '/mock-component.jsx.snap',
Expand All @@ -63,6 +73,16 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: '/mock-component.jsx.snap',
code: generateExportsSnapshotString(20),
options: [
{
externalMaxSize: 21,
inlineMaxSize: 19,
},
],
},
],
invalid: [
{
Expand All @@ -88,6 +108,20 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: 'mock.js',
code: generateExpectInlineSnapsCode(
50,
'toThrowErrorMatchingInlineSnapshot',
),
options: [{ externalMaxSize: 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',
Expand All @@ -111,6 +145,17 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
filename: '/mock-component.jsx.snap',
code: generateExportsSnapshotString(100),
options: [{ externalMaxSize: 70, inlineMaxSize: 101 }],
errors: [
{
messageId: 'tooLongSnapshots',
data: { lineLimit: 70, lineCount: 100 },
},
],
},
{
// "should report if maxSize is zero"
filename: '/mock-component.jsx.snap',
Expand Down
71 changes: 59 additions & 12 deletions src/rules/no-large-snapshots.ts
Expand Up @@ -13,6 +13,8 @@ import {

interface RuleOptions {
maxSize?: number;
inlineMaxSize?: number;
externalMaxSize?: number;
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
whitelistedSnapshots?: Record<string, Array<string | RegExp>>;
}

Expand All @@ -23,7 +25,10 @@ type RuleContext = TSESLint.RuleContext<MessageId, [RuleOptions]>;
const reportOnViolation = (
context: RuleContext,
node: TSESTree.CallExpression | TSESTree.ExpressionStatement,
{ maxSize: lineLimit = 50, whitelistedSnapshots = {} }: RuleOptions,
{
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
maxSize: lineLimit = 50,
whitelistedSnapshots = {},
}: Omit<RuleOptions, 'inlineMaxSize' | 'externalMaxSize'>,
) => {
const startLine = node.loc.start.line;
const endLine = node.loc.end.line;
Expand Down Expand Up @@ -71,6 +76,14 @@ 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 @@ -87,19 +100,45 @@ export default createRule<[RuleOptions], MessageId>({
type: 'suggestion',
schema: [
{
type: 'object',
properties: {
maxSize: {
type: 'number',
anyOf: [
{
oneOf: [
{
type: 'object',
properties: {
maxSize: {
type: 'number',
},
},
additionalProperties: false,
},
{
type: 'object',
properties: {
externalMaxSize: {
type: 'number',
},
inlineMaxSize: {
type: 'number',
},
},
additionalProperties: false,
},
],
},
whitelistedSnapshots: {
{
type: 'object',
patternProperties: {
'.*': { type: 'array' },
properties: {
whitelistedSnapshots: {
type: 'object',
patternProperties: {
'.*': { type: 'array' },
},
},
},
additionalProperties: false,
},
},
additionalProperties: false,
],
},
],
},
Expand All @@ -108,7 +147,11 @@ export default createRule<[RuleOptions], MessageId>({
if (context.getFilename().endsWith('.snap')) {
return {
ExpressionStatement(node) {
reportOnViolation(context, node, options);
reportOnViolation(
context,
node,
buildOptionsByMaxSizeAttributeName(options, 'externalMaxSize'),
);
},
};
} else if (context.getFilename().endsWith('.js')) {
Expand All @@ -125,7 +168,11 @@ export default createRule<[RuleOptions], MessageId>({
'toThrowErrorMatchingInlineSnapshot',
))
) {
reportOnViolation(context, node, options);
reportOnViolation(
context,
node,
buildOptionsByMaxSizeAttributeName(options, 'inlineMaxSize'),
);
}
},
};
Expand Down