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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-large-snapshots): run on all files regardless of type #637

Merged
merged 1 commit into from Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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,
});
}
},
};
},
});