no-wait-for-snapshot.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. const utils_1 = require("@typescript-eslint/utils");
  5. const create_testing_library_rule_1 = require("../create-testing-library-rule");
  6. const node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'no-wait-for-snapshot';
  8. const SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/;
  9. exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
  10. name: exports.RULE_NAME,
  11. meta: {
  12. type: 'problem',
  13. docs: {
  14. description: 'Ensures no snapshot is generated inside of a `waitFor` call',
  15. recommendedConfig: {
  16. dom: 'error',
  17. angular: 'error',
  18. react: 'error',
  19. vue: 'error',
  20. marko: 'error',
  21. },
  22. },
  23. messages: {
  24. noWaitForSnapshot: "A snapshot can't be generated inside of a `{{ name }}` call",
  25. },
  26. schema: [],
  27. },
  28. defaultOptions: [],
  29. create(context, _, helpers) {
  30. function getClosestAsyncUtil(node) {
  31. let n = node;
  32. do {
  33. const callExpression = (0, node_utils_1.findClosestCallExpressionNode)(n);
  34. if (!callExpression) {
  35. return null;
  36. }
  37. if (utils_1.ASTUtils.isIdentifier(callExpression.callee) &&
  38. helpers.isAsyncUtil(callExpression.callee)) {
  39. return callExpression.callee;
  40. }
  41. if ((0, node_utils_1.isMemberExpression)(callExpression.callee) &&
  42. utils_1.ASTUtils.isIdentifier(callExpression.callee.property) &&
  43. helpers.isAsyncUtil(callExpression.callee.property)) {
  44. return callExpression.callee.property;
  45. }
  46. if (callExpression.parent) {
  47. n = (0, node_utils_1.findClosestCallExpressionNode)(callExpression.parent);
  48. }
  49. } while (n !== null);
  50. return null;
  51. }
  52. return {
  53. [`Identifier[name=${String(SNAPSHOT_REGEXP)}]`](node) {
  54. const closestAsyncUtil = getClosestAsyncUtil(node);
  55. if (closestAsyncUtil === null) {
  56. return;
  57. }
  58. context.report({
  59. node,
  60. messageId: 'noWaitForSnapshot',
  61. data: { name: closestAsyncUtil.name },
  62. });
  63. },
  64. };
  65. },
  66. });