no-large-snapshots.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = require("path");
  7. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  8. var _utils = require("./utils");
  9. const reportOnViolation = (context, node, {
  10. maxSize: lineLimit = 50,
  11. allowedSnapshots = {}
  12. }) => {
  13. const startLine = node.loc.start.line;
  14. const endLine = node.loc.end.line;
  15. const lineCount = endLine - startLine;
  16. const allPathsAreAbsolute = Object.keys(allowedSnapshots).every(_path.isAbsolute);
  17. if (!allPathsAreAbsolute) {
  18. throw new Error('All paths for allowedSnapshots must be absolute. You can use JS config and `path.resolve`');
  19. }
  20. let isAllowed = false;
  21. if (node.type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement && 'left' in node.expression && (0, _utils.isExpectMember)(node.expression.left)) {
  22. const fileName = context.getFilename();
  23. const allowedSnapshotsInFile = allowedSnapshots[fileName];
  24. if (allowedSnapshotsInFile) {
  25. const snapshotName = (0, _utils.getAccessorValue)(node.expression.left.property);
  26. isAllowed = allowedSnapshotsInFile.some(name => {
  27. if (name instanceof RegExp) {
  28. return name.test(snapshotName);
  29. }
  30. return snapshotName === name;
  31. });
  32. }
  33. }
  34. if (!isAllowed && lineCount > lineLimit) {
  35. context.report({
  36. messageId: lineLimit === 0 ? 'noSnapshot' : 'tooLongSnapshots',
  37. data: {
  38. lineLimit,
  39. lineCount
  40. },
  41. node
  42. });
  43. }
  44. };
  45. var _default = (0, _utils.createRule)({
  46. name: __filename,
  47. meta: {
  48. docs: {
  49. category: 'Best Practices',
  50. description: 'disallow large snapshots',
  51. recommended: false
  52. },
  53. messages: {
  54. noSnapshot: '`{{ lineCount }}`s should begin with lowercase',
  55. tooLongSnapshots: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long'
  56. },
  57. type: 'suggestion',
  58. schema: [{
  59. type: 'object',
  60. properties: {
  61. maxSize: {
  62. type: 'number'
  63. },
  64. inlineMaxSize: {
  65. type: 'number'
  66. },
  67. allowedSnapshots: {
  68. type: 'object',
  69. additionalProperties: {
  70. type: 'array'
  71. }
  72. }
  73. },
  74. additionalProperties: false
  75. }]
  76. },
  77. defaultOptions: [{}],
  78. create(context, [options]) {
  79. if (context.getFilename().endsWith('.snap')) {
  80. return {
  81. ExpressionStatement(node) {
  82. reportOnViolation(context, node, options);
  83. }
  84. };
  85. }
  86. return {
  87. CallExpression(node) {
  88. var _matcher$arguments;
  89. if (!(0, _utils.isExpectCall)(node)) {
  90. return;
  91. }
  92. const {
  93. matcher
  94. } = (0, _utils.parseExpectCall)(node);
  95. if ((matcher === null || matcher === void 0 ? void 0 : matcher.node.parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
  96. return;
  97. }
  98. if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes(matcher.name) && (_matcher$arguments = matcher.arguments) !== null && _matcher$arguments !== void 0 && _matcher$arguments.length) {
  99. var _options$inlineMaxSiz;
  100. reportOnViolation(context, matcher.arguments[0], { ...options,
  101. maxSize: (_options$inlineMaxSiz = options.inlineMaxSize) !== null && _options$inlineMaxSiz !== void 0 ? _options$inlineMaxSiz : options.maxSize
  102. });
  103. }
  104. }
  105. };
  106. }
  107. });
  108. exports.default = _default;