Skip to content

Commit

Permalink
fix(no-large-snapshots): run on all files regardless of type (#637)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `no-large-snapshots` runs on all files regardless of type 

Fixes #370
  • Loading branch information
G-Rath authored and SimenB committed Aug 24, 2020
1 parent e12eab9 commit a99e9ae
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
19 changes: 14 additions & 5 deletions src/rules/__tests__/no-large-snapshots.test.ts
Expand Up @@ -24,6 +24,9 @@ const generateExpectInlineSnapsCode = (

ruleTester.run('no-large-snapshots', rule, {
valid: [
'expect(something)',
'expect(something).toBe(1)',
'expect(something).toMatchInlineSnapshot',
{
filename: 'mock.js',
code: generateExpectInlineSnapsCode(2, 'toMatchInlineSnapshot'),
Expand All @@ -35,11 +38,6 @@ ruleTester.run('no-large-snapshots', rule, {
'toThrowErrorMatchingInlineSnapshot',
),
},
{
// "it should return an empty object for non snapshot files"
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
},
{
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'),
Expand Down Expand Up @@ -125,6 +123,17 @@ ruleTester.run('no-large-snapshots', rule, {
},
],
},
{
// "it should return an empty object for non snapshot files"
filename: 'mock.jsx',
code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'),
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 Down
50 changes: 27 additions & 23 deletions src/rules/no-large-snapshots.ts
Expand Up @@ -7,8 +7,9 @@ import {
import {
createRule,
getAccessorValue,
isExpectCall,
isExpectMember,
isSupportedAccessor,
parseExpectCall,
} from './utils';

interface RuleOptions {
Expand Down Expand Up @@ -111,29 +112,32 @@ export default createRule<[RuleOptions], MessageId>({
reportOnViolation(context, node, options);
},
};
} else if (context.getFilename().endsWith('.js')) {
return {
CallExpression(node) {
if (
'property' in node.callee &&
(isSupportedAccessor(
node.callee.property,
'toMatchInlineSnapshot',
) ||
isSupportedAccessor(
node.callee.property,
'toThrowErrorMatchingInlineSnapshot',
))
) {
reportOnViolation(context, node, {
...options,
maxSize: options.inlineMaxSize ?? options.maxSize,
});
}
},
};
}

return {};
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}

const { matcher } = parseExpectCall(node);

if (matcher?.node.parent?.type !== AST_NODE_TYPES.CallExpression) {
return;
}

if (
[
'toMatchInlineSnapshot',
'toThrowErrorMatchingInlineSnapshot',
].includes(matcher.name)
) {
reportOnViolation(context, matcher.node.parent, {
...options,
maxSize: options.inlineMaxSize ?? options.maxSize,
});
}
},
};
},
});

0 comments on commit a99e9ae

Please sign in to comment.