no-identical-title.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const newDescribeContext = () => ({
  8. describeTitles: [],
  9. testTitles: []
  10. });
  11. var _default = (0, _utils.createRule)({
  12. name: __filename,
  13. meta: {
  14. docs: {
  15. category: 'Best Practices',
  16. description: 'Disallow identical titles',
  17. recommended: 'error'
  18. },
  19. messages: {
  20. multipleTestTitle: 'Test title is used multiple times in the same describe block.',
  21. multipleDescribeTitle: 'Describe block title is used multiple times in the same describe block.'
  22. },
  23. schema: [],
  24. type: 'suggestion'
  25. },
  26. defaultOptions: [],
  27. create(context) {
  28. const contexts = [newDescribeContext()];
  29. return {
  30. CallExpression(node) {
  31. const currentLayer = contexts[contexts.length - 1];
  32. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  33. if (!jestFnCall) {
  34. return;
  35. }
  36. if (jestFnCall.type === 'describe') {
  37. contexts.push(newDescribeContext());
  38. }
  39. if (jestFnCall.members.find(s => (0, _utils.isSupportedAccessor)(s, 'each'))) {
  40. return;
  41. }
  42. const [argument] = node.arguments;
  43. if (!argument || !(0, _utils.isStringNode)(argument)) {
  44. return;
  45. }
  46. const title = (0, _utils.getStringValue)(argument);
  47. if (jestFnCall.type === 'test') {
  48. if (currentLayer.testTitles.includes(title)) {
  49. context.report({
  50. messageId: 'multipleTestTitle',
  51. node: argument
  52. });
  53. }
  54. currentLayer.testTitles.push(title);
  55. }
  56. if (jestFnCall.type !== 'describe') {
  57. return;
  58. }
  59. if (currentLayer.describeTitles.includes(title)) {
  60. context.report({
  61. messageId: 'multipleDescribeTitle',
  62. node: argument
  63. });
  64. }
  65. currentLayer.describeTitles.push(title);
  66. },
  67. 'CallExpression:exit'(node) {
  68. if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe'])) {
  69. contexts.pop();
  70. }
  71. }
  72. };
  73. }
  74. });
  75. exports.default = _default;