no-large-snapshots.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = require("path");
  7. var _utils = require("@typescript-eslint/utils");
  8. var _utils2 = 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 === _utils.AST_NODE_TYPES.ExpressionStatement && 'left' in node.expression && node.expression.left.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.expression.left.property)) {
  22. const fileName = context.getFilename();
  23. const allowedSnapshotsInFile = allowedSnapshots[fileName];
  24. if (allowedSnapshotsInFile) {
  25. const snapshotName = (0, _utils2.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, _utils2.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. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  89. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  90. return;
  91. }
  92. if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes((0, _utils2.getAccessorValue)(jestFnCall.matcher)) && jestFnCall.args.length) {
  93. var _options$inlineMaxSiz;
  94. reportOnViolation(context, jestFnCall.args[0], { ...options,
  95. maxSize: (_options$inlineMaxSiz = options.inlineMaxSize) !== null && _options$inlineMaxSiz !== void 0 ? _options$inlineMaxSiz : options.maxSize
  96. });
  97. }
  98. }
  99. };
  100. }
  101. });
  102. exports.default = _default;