require-top-level-describe.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const messages = {
  8. tooManyDescribes: 'There should not be more than {{ max }} describe{{ s }} at the top level',
  9. unexpectedTestCase: 'All test cases must be wrapped in a describe block.',
  10. unexpectedHook: 'All hooks must be wrapped in a describe block.'
  11. };
  12. var _default = (0, _utils.createRule)({
  13. name: __filename,
  14. meta: {
  15. docs: {
  16. category: 'Best Practices',
  17. description: 'Require test cases and hooks to be inside a `describe` block',
  18. recommended: false
  19. },
  20. messages,
  21. type: 'suggestion',
  22. schema: [{
  23. type: 'object',
  24. properties: {
  25. maxNumberOfTopLevelDescribes: {
  26. type: 'number',
  27. minimum: 1
  28. }
  29. },
  30. additionalProperties: false
  31. }]
  32. },
  33. defaultOptions: [{}],
  34. create(context) {
  35. var _context$options$;
  36. const {
  37. maxNumberOfTopLevelDescribes = Infinity
  38. } = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
  39. let numberOfTopLevelDescribeBlocks = 0;
  40. let numberOfDescribeBlocks = 0;
  41. return {
  42. CallExpression(node) {
  43. if ((0, _utils.isDescribeCall)(node)) {
  44. numberOfDescribeBlocks++;
  45. if (numberOfDescribeBlocks === 1) {
  46. numberOfTopLevelDescribeBlocks++;
  47. if (numberOfTopLevelDescribeBlocks > maxNumberOfTopLevelDescribes) {
  48. context.report({
  49. node,
  50. messageId: 'tooManyDescribes',
  51. data: {
  52. max: maxNumberOfTopLevelDescribes,
  53. s: maxNumberOfTopLevelDescribes === 1 ? '' : 's'
  54. }
  55. });
  56. }
  57. }
  58. return;
  59. }
  60. if (numberOfDescribeBlocks === 0) {
  61. if ((0, _utils.isTestCaseCall)(node)) {
  62. context.report({
  63. node,
  64. messageId: 'unexpectedTestCase'
  65. });
  66. return;
  67. }
  68. if ((0, _utils.isHook)(node)) {
  69. context.report({
  70. node,
  71. messageId: 'unexpectedHook'
  72. });
  73. return;
  74. }
  75. }
  76. },
  77. 'CallExpression:exit'(node) {
  78. if ((0, _utils.isDescribeCall)(node)) {
  79. numberOfDescribeBlocks--;
  80. }
  81. }
  82. };
  83. }
  84. });
  85. exports.default = _default;