Skip to content

Commit 22113db

Browse files
G-RathSimenB
authored andcommitted
fix(no-large-snapshots): run on all files regardless of type (#637)
BREAKING CHANGE: `no-large-snapshots` runs on all files regardless of type Fixes #370
1 parent 417f01d commit 22113db

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/rules/__tests__/no-large-snapshots.test.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const generateExpectInlineSnapsCode = (
2424

2525
ruleTester.run('no-large-snapshots', rule, {
2626
valid: [
27+
'expect(something)',
28+
'expect(something).toBe(1)',
29+
'expect(something).toMatchInlineSnapshot',
2730
{
2831
filename: 'mock.js',
2932
code: generateExpectInlineSnapsCode(2, 'toMatchInlineSnapshot'),
@@ -35,11 +38,6 @@ ruleTester.run('no-large-snapshots', rule, {
3538
'toThrowErrorMatchingInlineSnapshot',
3639
),
3740
},
38-
{
39-
// "it should return an empty object for non snapshot files"
40-
filename: 'mock.jsx',
41-
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
42-
},
4341
{
4442
filename: 'mock.jsx',
4543
code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'),
@@ -125,6 +123,17 @@ ruleTester.run('no-large-snapshots', rule, {
125123
},
126124
],
127125
},
126+
{
127+
// "it should return an empty object for non snapshot files"
128+
filename: 'mock.jsx',
129+
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
130+
errors: [
131+
{
132+
messageId: 'tooLongSnapshots',
133+
data: { lineLimit: 50, lineCount: 51 },
134+
},
135+
],
136+
},
128137
{
129138
// "should report if node has more than 50 lines of code, and no sizeThreshold option is passed"
130139
filename: '/mock-component.jsx.snap',

src/rules/no-large-snapshots.ts

+27-23
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
import {
88
createRule,
99
getAccessorValue,
10+
isExpectCall,
1011
isExpectMember,
11-
isSupportedAccessor,
12+
parseExpectCall,
1213
} from './utils';
1314

1415
interface RuleOptions {
@@ -111,29 +112,32 @@ export default createRule<[RuleOptions], MessageId>({
111112
reportOnViolation(context, node, options);
112113
},
113114
};
114-
} else if (context.getFilename().endsWith('.js')) {
115-
return {
116-
CallExpression(node) {
117-
if (
118-
'property' in node.callee &&
119-
(isSupportedAccessor(
120-
node.callee.property,
121-
'toMatchInlineSnapshot',
122-
) ||
123-
isSupportedAccessor(
124-
node.callee.property,
125-
'toThrowErrorMatchingInlineSnapshot',
126-
))
127-
) {
128-
reportOnViolation(context, node, {
129-
...options,
130-
maxSize: options.inlineMaxSize ?? options.maxSize,
131-
});
132-
}
133-
},
134-
};
135115
}
136116

137-
return {};
117+
return {
118+
CallExpression(node) {
119+
if (!isExpectCall(node)) {
120+
return;
121+
}
122+
123+
const { matcher } = parseExpectCall(node);
124+
125+
if (matcher?.node.parent?.type !== AST_NODE_TYPES.CallExpression) {
126+
return;
127+
}
128+
129+
if (
130+
[
131+
'toMatchInlineSnapshot',
132+
'toThrowErrorMatchingInlineSnapshot',
133+
].includes(matcher.name)
134+
) {
135+
reportOnViolation(context, matcher.node.parent, {
136+
...options,
137+
maxSize: options.inlineMaxSize ?? options.maxSize,
138+
});
139+
}
140+
},
141+
};
138142
},
139143
});

0 commit comments

Comments
 (0)