require-to-throw-message.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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: 'Require a message for `toThrow()`',
  13. recommended: false
  14. },
  15. messages: {
  16. addErrorMessage: 'Add an error message to {{ matcherName }}()'
  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. const {
  30. matcher
  31. } = jestFnCall;
  32. const matcherName = (0, _utils.getAccessorValue)(matcher);
  33. if (jestFnCall.args.length === 0 && ['toThrow', 'toThrowError'].includes(matcherName) && !jestFnCall.modifiers.some(nod => (0, _utils.getAccessorValue)(nod) === 'not')) {
  34. // Look for `toThrow` calls with no arguments.
  35. context.report({
  36. messageId: 'addErrorMessage',
  37. data: {
  38. matcherName
  39. },
  40. node: matcher
  41. });
  42. }
  43. }
  44. };
  45. }
  46. });
  47. exports.default = _default;