no-disabled-tests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: 'Disallow disabled tests',
  13. recommended: 'warn'
  14. },
  15. messages: {
  16. missingFunction: 'Test is missing function argument',
  17. pending: 'Call to pending()',
  18. pendingSuite: 'Call to pending() within test suite',
  19. pendingTest: 'Call to pending() within test',
  20. disabledSuite: 'Disabled test suite',
  21. disabledTest: 'Disabled test'
  22. },
  23. schema: [],
  24. type: 'suggestion'
  25. },
  26. defaultOptions: [],
  27. create(context) {
  28. let suiteDepth = 0;
  29. let testDepth = 0;
  30. return {
  31. CallExpression(node) {
  32. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  33. if (!jestFnCall) {
  34. return;
  35. }
  36. if (jestFnCall.type === 'describe') {
  37. suiteDepth++;
  38. }
  39. if (jestFnCall.type === 'test') {
  40. testDepth++;
  41. if (node.arguments.length < 2 && jestFnCall.members.every(s => (0, _utils.getAccessorValue)(s) !== 'todo')) {
  42. context.report({
  43. messageId: 'missingFunction',
  44. node
  45. });
  46. }
  47. }
  48. if ( // the only jest functions that are with "x" are "xdescribe", "xtest", and "xit"
  49. jestFnCall.name.startsWith('x') || jestFnCall.members.some(s => (0, _utils.getAccessorValue)(s) === 'skip')) {
  50. context.report({
  51. messageId: jestFnCall.type === 'describe' ? 'disabledSuite' : 'disabledTest',
  52. node
  53. });
  54. }
  55. },
  56. 'CallExpression:exit'(node) {
  57. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  58. if (!jestFnCall) {
  59. return;
  60. }
  61. if (jestFnCall.type === 'describe') {
  62. suiteDepth--;
  63. }
  64. if (jestFnCall.type === 'test') {
  65. testDepth--;
  66. }
  67. },
  68. 'CallExpression[callee.name="pending"]'(node) {
  69. if ((0, _utils.scopeHasLocalReference)(context.getScope(), 'pending')) {
  70. return;
  71. }
  72. if (testDepth > 0) {
  73. context.report({
  74. messageId: 'pendingTest',
  75. node
  76. });
  77. } else if (suiteDepth > 0) {
  78. context.report({
  79. messageId: 'pendingSuite',
  80. node
  81. });
  82. } else {
  83. context.report({
  84. messageId: 'pending',
  85. node
  86. });
  87. }
  88. }
  89. };
  90. }
  91. });
  92. exports.default = _default;