no-focused-tests.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. var _utils2 = require("./utils");
  8. var _default = (0, _utils2.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Disallow focused tests',
  14. recommended: 'error',
  15. suggestion: true
  16. },
  17. messages: {
  18. focusedTest: 'Unexpected focused test.',
  19. suggestRemoveFocus: 'Remove focus from test.'
  20. },
  21. schema: [],
  22. type: 'suggestion',
  23. hasSuggestions: true
  24. },
  25. defaultOptions: [],
  26. create(context) {
  27. return {
  28. CallExpression(node) {
  29. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  30. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe') {
  31. return;
  32. }
  33. if (jestFnCall.name.startsWith('f')) {
  34. context.report({
  35. messageId: 'focusedTest',
  36. node,
  37. suggest: [{
  38. messageId: 'suggestRemoveFocus',
  39. fix(fixer) {
  40. // don't apply the fixer if we're an aliased import
  41. if (jestFnCall.head.type === 'import' && jestFnCall.name !== jestFnCall.head.local) {
  42. return null;
  43. }
  44. return fixer.removeRange([node.range[0], node.range[0] + 1]);
  45. }
  46. }]
  47. });
  48. return;
  49. }
  50. const onlyNode = jestFnCall.members.find(s => (0, _utils2.getAccessorValue)(s) === 'only');
  51. if (!onlyNode) {
  52. return;
  53. }
  54. context.report({
  55. messageId: 'focusedTest',
  56. node: onlyNode,
  57. suggest: [{
  58. messageId: 'suggestRemoveFocus',
  59. fix: fixer => fixer.removeRange([onlyNode.range[0] - 1, onlyNode.range[1] + Number(onlyNode.type !== _utils.AST_NODE_TYPES.Identifier)])
  60. }]
  61. });
  62. }
  63. };
  64. }
  65. });
  66. exports.default = _default;