prefer-snapshot-hint.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const snapshotMatchers = ['toMatchSnapshot', 'toThrowErrorMatchingSnapshot'];
  8. const snapshotMatcherNames = snapshotMatchers;
  9. const isSnapshotMatcherWithoutHint = expectFnCall => {
  10. if (expectFnCall.args.length === 0) {
  11. return true;
  12. } // this matcher only supports one argument which is the hint
  13. if (!(0, _utils.isSupportedAccessor)(expectFnCall.matcher, 'toMatchSnapshot')) {
  14. return expectFnCall.args.length !== 1;
  15. } // if we're being passed two arguments,
  16. // the second one should be the hint
  17. if (expectFnCall.args.length === 2) {
  18. return false;
  19. }
  20. const [arg] = expectFnCall.args; // the first argument to `toMatchSnapshot` can be _either_ a snapshot hint or
  21. // an object with asymmetric matchers, so we can't just assume that the first
  22. // argument is a hint when it's by itself.
  23. return !(0, _utils.isStringNode)(arg);
  24. };
  25. const messages = {
  26. missingHint: 'You should provide a hint for this snapshot'
  27. };
  28. var _default = (0, _utils.createRule)({
  29. name: __filename,
  30. meta: {
  31. docs: {
  32. category: 'Best Practices',
  33. description: 'Prefer including a hint with external snapshots',
  34. recommended: false
  35. },
  36. messages,
  37. type: 'suggestion',
  38. schema: [{
  39. type: 'string',
  40. enum: ['always', 'multi']
  41. }]
  42. },
  43. defaultOptions: ['multi'],
  44. create(context, [mode]) {
  45. const snapshotMatchers = [];
  46. const depths = [];
  47. let expressionDepth = 0;
  48. const reportSnapshotMatchersWithoutHints = () => {
  49. for (const snapshotMatcher of snapshotMatchers) {
  50. if (isSnapshotMatcherWithoutHint(snapshotMatcher)) {
  51. context.report({
  52. messageId: 'missingHint',
  53. node: snapshotMatcher.matcher
  54. });
  55. }
  56. }
  57. };
  58. const enterExpression = () => {
  59. expressionDepth++;
  60. };
  61. const exitExpression = () => {
  62. expressionDepth--;
  63. if (mode === 'always') {
  64. reportSnapshotMatchersWithoutHints();
  65. snapshotMatchers.length = 0;
  66. }
  67. if (mode === 'multi' && expressionDepth === 0) {
  68. if (snapshotMatchers.length > 1) {
  69. reportSnapshotMatchersWithoutHints();
  70. }
  71. snapshotMatchers.length = 0;
  72. }
  73. };
  74. return {
  75. 'Program:exit'() {
  76. enterExpression();
  77. exitExpression();
  78. },
  79. FunctionExpression: enterExpression,
  80. 'FunctionExpression:exit': exitExpression,
  81. ArrowFunctionExpression: enterExpression,
  82. 'ArrowFunctionExpression:exit': exitExpression,
  83. 'CallExpression:exit'(node) {
  84. if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe', 'test'])) {
  85. var _depths$pop;
  86. /* istanbul ignore next */
  87. expressionDepth = (_depths$pop = depths.pop()) !== null && _depths$pop !== void 0 ? _depths$pop : 0;
  88. }
  89. },
  90. CallExpression(node) {
  91. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  92. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  93. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'describe' || (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test') {
  94. depths.push(expressionDepth);
  95. expressionDepth = 0;
  96. }
  97. return;
  98. }
  99. const matcherName = (0, _utils.getAccessorValue)(jestFnCall.matcher);
  100. if (!snapshotMatcherNames.includes(matcherName)) {
  101. return;
  102. }
  103. snapshotMatchers.push(jestFnCall);
  104. }
  105. };
  106. }
  107. });
  108. exports.default = _default;