prefer-called-with.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. var _default = (0, _utils.createRule)({
  8. name: __filename,
  9. meta: {
  10. docs: {
  11. category: 'Best Practices',
  12. description: 'Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`',
  13. recommended: false
  14. },
  15. messages: {
  16. preferCalledWith: 'Prefer {{ matcherName }}With(/* expected args */)'
  17. },
  18. type: 'suggestion',
  19. schema: []
  20. },
  21. defaultOptions: [],
  22. create(context) {
  23. return {
  24. CallExpression(node) {
  25. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  26. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  27. return;
  28. }
  29. if (jestFnCall.modifiers.some(nod => (0, _utils.getAccessorValue)(nod) === 'not')) {
  30. return;
  31. }
  32. const {
  33. matcher
  34. } = jestFnCall;
  35. const matcherName = (0, _utils.getAccessorValue)(matcher);
  36. if (['toBeCalled', 'toHaveBeenCalled'].includes(matcherName)) {
  37. context.report({
  38. data: {
  39. matcherName
  40. },
  41. messageId: 'preferCalledWith',
  42. node: matcher
  43. });
  44. }
  45. }
  46. };
  47. }
  48. });
  49. exports.default = _default;