Skip to content

Commit 9137c21

Browse files
JohanBrorsonSimenB
authored andcommittedOct 24, 2018
fix(no-large-snapshots): support inline snapshots (#186)
1 parent 225a0e1 commit 9137c21

File tree

3 files changed

+90
-22
lines changed

3 files changed

+90
-22
lines changed
 

‎docs/rules/no-large-snapshots.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ parserOptions: {
2222

2323
## Rule Details
2424

25-
This rule looks at all Jest snapshot files (files with `.snap` extension) and
26-
validates that each stored snapshot within those files does not exceed 50 lines
27-
(by default, this is configurable as explained in `Options` section below).
25+
This rule looks at all Jest inline and external snapshots (files with `.snap`
26+
extension) and validates that each stored snapshot within those files does not
27+
exceed 50 lines (by default, this is configurable as explained in `Options`
28+
section below).
2829

2930
Example of **incorrect** code for this rule:
3031

‎rules/__tests__/no-large-snapshots.test.js

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
11
'use strict';
22

3-
const noLargeSnapshots = require('../no-large-snapshots').create;
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../no-large-snapshots');
5+
const noLargeSnapshots = rule.create;
6+
7+
const ruleTester = new RuleTester({
8+
parserOptions: {
9+
ecmaVersion: 2015,
10+
},
11+
});
12+
13+
ruleTester.run('no-large-snapshots', rule, {
14+
valid: [
15+
{
16+
filename: 'mock.js',
17+
code: `expect(something).toMatchInlineSnapshot(\`\n${'line\n'.repeat(
18+
2
19+
)}\`);`,
20+
},
21+
{
22+
filename: 'mock.js',
23+
code: `expect(something).toThrowErrorMatchingInlineSnapshot(\`\n${'line\n'.repeat(
24+
2
25+
)}\`);`,
26+
},
27+
],
28+
invalid: [
29+
{
30+
filename: 'mock.js',
31+
code: `expect(something).toMatchInlineSnapshot(\`\n${'line\n'.repeat(
32+
50
33+
)}\`);`,
34+
errors: [
35+
{
36+
message:
37+
'Expected Jest snapshot to be smaller than 50 lines but was 51 lines long',
38+
},
39+
],
40+
},
41+
{
42+
filename: 'mock.js',
43+
code: `expect(something).toThrowErrorMatchingInlineSnapshot(\`\n${'line\n'.repeat(
44+
50
45+
)}\`);`,
46+
errors: [
47+
{
48+
message:
49+
'Expected Jest snapshot to be smaller than 50 lines but was 51 lines long',
50+
},
51+
],
52+
},
53+
],
54+
});
455

556
// was not able to use https://eslint.org/docs/developer-guide/nodejs-api#ruletester for these because there is no way to configure RuleTester to run non .js files
657
describe('no-large-snapshots', () => {

‎rules/no-large-snapshots.js

+34-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
const getDocsUrl = require('./util').getDocsUrl;
44

5+
const reportOnViolation = (context, node) => {
6+
const lineLimit =
7+
context.options[0] && Number.isFinite(context.options[0].maxSize)
8+
? context.options[0].maxSize
9+
: 50;
10+
const startLine = node.loc.start.line;
11+
const endLine = node.loc.end.line;
12+
const lineCount = endLine - startLine;
13+
14+
if (lineCount > lineLimit) {
15+
context.report({
16+
message:
17+
lineLimit === 0
18+
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long'
19+
: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
20+
data: { lineLimit, lineCount },
21+
node,
22+
});
23+
}
24+
};
25+
526
module.exports = {
627
meta: {
728
docs: {
@@ -10,26 +31,21 @@ module.exports = {
1031
},
1132
create(context) {
1233
if (context.getFilename().endsWith('.snap')) {
13-
const lineLimit =
14-
context.options[0] && Number.isFinite(context.options[0].maxSize)
15-
? context.options[0].maxSize
16-
: 50;
17-
1834
return {
1935
ExpressionStatement(node) {
20-
const startLine = node.loc.start.line;
21-
const endLine = node.loc.end.line;
22-
const lineCount = endLine - startLine;
23-
24-
if (lineCount > lineLimit) {
25-
context.report({
26-
message:
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',
30-
data: { lineLimit, lineCount },
31-
node,
32-
});
36+
reportOnViolation(context, node);
37+
},
38+
};
39+
} else if (context.getFilename().endsWith('.js')) {
40+
return {
41+
CallExpression(node) {
42+
const propertyName =
43+
node.callee.property && node.callee.property.name;
44+
if (
45+
propertyName === 'toMatchInlineSnapshot' ||
46+
propertyName === 'toThrowErrorMatchingInlineSnapshot'
47+
) {
48+
reportOnViolation(context, node);
3349
}
3450
},
3551
};

0 commit comments

Comments
 (0)