eslint-plugin-jest
Advanced tools
Comparing version 21.26.0 to 21.26.1
@@ -25,5 +25,6 @@ # disallow large snapshots (no-large-snapshots) | ||
This rule looks at all Jest snapshot files (files with `.snap` extension) and | ||
validates that each stored snapshot within those files does not exceed 50 lines | ||
(by default, this is configurable as explained in `Options` section below). | ||
This rule looks at all Jest inline and external snapshots (files with `.snap` | ||
extension) and validates that each stored snapshot within those files does not | ||
exceed 50 lines (by default, this is configurable as explained in `Options` | ||
section below). | ||
@@ -30,0 +31,0 @@ Example of **incorrect** code for this rule: |
{ | ||
"name": "eslint-plugin-jest", | ||
"version": "21.26.0", | ||
"version": "21.26.1", | ||
"description": "Eslint rules for Jest", | ||
@@ -5,0 +5,0 @@ "repository": "jest-community/eslint-plugin-jest", |
'use strict'; | ||
const noLargeSnapshots = require('../no-large-snapshots').create; | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-large-snapshots'); | ||
const noLargeSnapshots = rule.create; | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
}, | ||
}); | ||
ruleTester.run('no-large-snapshots', rule, { | ||
valid: [ | ||
{ | ||
filename: 'mock.js', | ||
code: `expect(something).toMatchInlineSnapshot(\`\n${'line\n'.repeat( | ||
2 | ||
)}\`);`, | ||
}, | ||
{ | ||
filename: 'mock.js', | ||
code: `expect(something).toThrowErrorMatchingInlineSnapshot(\`\n${'line\n'.repeat( | ||
2 | ||
)}\`);`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'mock.js', | ||
code: `expect(something).toMatchInlineSnapshot(\`\n${'line\n'.repeat( | ||
50 | ||
)}\`);`, | ||
errors: [ | ||
{ | ||
message: | ||
'Expected Jest snapshot to be smaller than 50 lines but was 51 lines long', | ||
}, | ||
], | ||
}, | ||
{ | ||
filename: 'mock.js', | ||
code: `expect(something).toThrowErrorMatchingInlineSnapshot(\`\n${'line\n'.repeat( | ||
50 | ||
)}\`);`, | ||
errors: [ | ||
{ | ||
message: | ||
'Expected Jest snapshot to be smaller than 50 lines but was 51 lines long', | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
// 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 | ||
@@ -6,0 +57,0 @@ describe('no-large-snapshots', () => { |
@@ -5,2 +5,23 @@ 'use strict'; | ||
const reportOnViolation = (context, node) => { | ||
const lineLimit = | ||
context.options[0] && Number.isFinite(context.options[0].maxSize) | ||
? context.options[0].maxSize | ||
: 50; | ||
const startLine = node.loc.start.line; | ||
const endLine = node.loc.end.line; | ||
const lineCount = endLine - startLine; | ||
if (lineCount > lineLimit) { | ||
context.report({ | ||
message: | ||
lineLimit === 0 | ||
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long' | ||
: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long', | ||
data: { lineLimit, lineCount }, | ||
node, | ||
}); | ||
} | ||
}; | ||
module.exports = { | ||
@@ -14,22 +35,17 @@ meta: { | ||
if (context.getFilename().endsWith('.snap')) { | ||
const lineLimit = | ||
context.options[0] && Number.isFinite(context.options[0].maxSize) | ||
? context.options[0].maxSize | ||
: 50; | ||
return { | ||
ExpressionStatement(node) { | ||
const startLine = node.loc.start.line; | ||
const endLine = node.loc.end.line; | ||
const lineCount = endLine - startLine; | ||
if (lineCount > lineLimit) { | ||
context.report({ | ||
message: | ||
lineLimit === 0 | ||
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long' | ||
: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long', | ||
data: { lineLimit, lineCount }, | ||
node, | ||
}); | ||
reportOnViolation(context, node); | ||
}, | ||
}; | ||
} else if (context.getFilename().endsWith('.js')) { | ||
return { | ||
CallExpression(node) { | ||
const propertyName = | ||
node.callee.property && node.callee.property.name; | ||
if ( | ||
propertyName === 'toMatchInlineSnapshot' || | ||
propertyName === 'toThrowErrorMatchingInlineSnapshot' | ||
) { | ||
reportOnViolation(context, node); | ||
} | ||
@@ -36,0 +52,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
181948
5178