Skip to content

Commit e42c9e3

Browse files
paularmstrongSimenB
authored andcommittedAug 9, 2018
fix(rule): no-large-snapshots to allow maxSize of 0 (#132)
1 parent 3aa7c99 commit e42c9e3

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
 

Diff for: ‎rules/__tests__/__snapshots__/no-large-snapshots.test.js.snap

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`no-large-snapshots ExpressionStatement function should report if maxSize is zero 1`] = `
4+
Array [
5+
Object {
6+
"data": Object {
7+
"lineCount": 1,
8+
"lineLimit": 0,
9+
},
10+
"message": "Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long",
11+
"node": Object {
12+
"loc": Object {
13+
"end": Object {
14+
"line": 2,
15+
},
16+
"start": Object {
17+
"line": 1,
18+
},
19+
},
20+
},
21+
},
22+
]
23+
`;
24+
325
exports[`no-large-snapshots ExpressionStatement function should report if node has more lines of code than number given in sizeThreshold option 1`] = `
426
Array [
527
Object {

Diff for: ‎rules/__tests__/no-large-snapshots.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ describe('no-large-snapshots', () => {
7474
expect(mockReport.mock.calls[0]).toMatchSnapshot();
7575
});
7676

77+
it('should report if maxSize is zero', () => {
78+
const mockReport = jest.fn();
79+
const mockContext = {
80+
getFilename: () => 'mock-component.jsx.snap',
81+
options: [{ maxSize: 0 }],
82+
report: mockReport,
83+
};
84+
const mockNode = {
85+
loc: {
86+
start: {
87+
line: 1,
88+
},
89+
end: {
90+
line: 2,
91+
},
92+
},
93+
};
94+
noLargeSnapshots(mockContext).ExpressionStatement(mockNode);
95+
96+
expect(mockReport).toHaveBeenCalledTimes(1);
97+
expect(mockReport.mock.calls[0]).toMatchSnapshot();
98+
});
99+
77100
it('should not report if node has fewer lines of code than limit', () => {
78101
const mockReport = jest.fn();
79102
const mockContext = {

Diff for: ‎rules/no-large-snapshots.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = {
1111
create(context) {
1212
if (context.getFilename().endsWith('.snap')) {
1313
const lineLimit =
14-
(context.options[0] && context.options[0].maxSize) || 50;
14+
context.options[0] && Number.isFinite(context.options[0].maxSize)
15+
? context.options[0].maxSize
16+
: 50;
1517

1618
return {
1719
ExpressionStatement(node) {
@@ -22,7 +24,9 @@ module.exports = {
2224
if (lineCount > lineLimit) {
2325
context.report({
2426
message:
25-
'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
27+
lineLimit === 0
28+
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long'
29+
: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
2630
data: { lineLimit, lineCount },
2731
node,
2832
});

0 commit comments

Comments
 (0)
Please sign in to comment.