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

fix: no-large-snapshots to allow maxSize of 0 #132

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
22 changes: 22 additions & 0 deletions rules/__tests__/__snapshots__/no-large-snapshots.test.js.snap
@@ -1,5 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`no-large-snapshots ExpressionStatement function should report if maxSize is zero 1`] = `
Array [
Object {
"data": Object {
"lineCount": 1,
"lineLimit": 0,
},
"message": "Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long",
"node": Object {
"loc": Object {
"end": Object {
"line": 2,
},
"start": Object {
"line": 1,
},
},
},
},
]
`;

exports[`no-large-snapshots ExpressionStatement function should report if node has more lines of code than number given in sizeThreshold option 1`] = `
Array [
Object {
Expand Down
23 changes: 23 additions & 0 deletions rules/__tests__/no-large-snapshots.test.js
Expand Up @@ -74,6 +74,29 @@ describe('no-large-snapshots', () => {
expect(mockReport.mock.calls[0]).toMatchSnapshot();
});

it('should report if maxSize is zero', () => {
const mockReport = jest.fn();
const mockContext = {
getFilename: () => 'mock-component.jsx.snap',
options: [{ maxSize: 0 }],
report: mockReport,
};
const mockNode = {
loc: {
start: {
line: 1,
},
end: {
line: 2,
},
},
};
noLargeSnapshots(mockContext).ExpressionStatement(mockNode);

expect(mockReport).toHaveBeenCalledTimes(1);
expect(mockReport.mock.calls[0]).toMatchSnapshot();
});

it('should not report if node has fewer lines of code than limit', () => {
const mockReport = jest.fn();
const mockContext = {
Expand Down
8 changes: 6 additions & 2 deletions rules/no-large-snapshots.js
Expand Up @@ -11,7 +11,9 @@ module.exports = {
create(context) {
if (context.getFilename().endsWith('.snap')) {
const lineLimit =
(context.options[0] && context.options[0].maxSize) || 50;
context.options[0] && Number.isFinite(context.options[0].maxSize)
? context.options[0].maxSize
: 50;

return {
ExpressionStatement(node) {
Expand All @@ -22,7 +24,9 @@ module.exports = {
if (lineCount > lineLimit) {
context.report({
message:
'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
lineLimit === 0
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one!

: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
data: { lineLimit, lineCount },
node,
});
Expand Down