no-standalone-expect.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const getBlockType = (statement, context) => {
  9. const func = statement.parent;
  10. /* istanbul ignore if */
  11. if (!func) {
  12. throw new Error(`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
  13. } // functionDeclaration: function func() {}
  14. if (func.type === _utils.AST_NODE_TYPES.FunctionDeclaration) {
  15. return 'function';
  16. }
  17. if ((0, _utils2.isFunction)(func) && func.parent) {
  18. const expr = func.parent; // arrow function or function expr
  19. if (expr.type === _utils.AST_NODE_TYPES.VariableDeclarator) {
  20. return 'function';
  21. } // if it's not a variable, it will be callExpr, we only care about describe
  22. if (expr.type === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(expr, context, ['describe'])) {
  23. return 'describe';
  24. }
  25. }
  26. return null;
  27. };
  28. var _default = (0, _utils2.createRule)({
  29. name: __filename,
  30. meta: {
  31. docs: {
  32. category: 'Best Practices',
  33. description: 'Disallow using `expect` outside of `it` or `test` blocks',
  34. recommended: 'error'
  35. },
  36. messages: {
  37. unexpectedExpect: 'Expect must be inside of a test block.'
  38. },
  39. type: 'suggestion',
  40. schema: [{
  41. properties: {
  42. additionalTestBlockFunctions: {
  43. type: 'array',
  44. items: {
  45. type: 'string'
  46. }
  47. }
  48. },
  49. additionalProperties: false
  50. }]
  51. },
  52. defaultOptions: [{
  53. additionalTestBlockFunctions: []
  54. }],
  55. create(context, [{
  56. additionalTestBlockFunctions = []
  57. }]) {
  58. const callStack = [];
  59. const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils2.getNodeName)(node) || '');
  60. return {
  61. CallExpression(node) {
  62. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  63. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'expect') {
  64. var _jestFnCall$head$node;
  65. if (((_jestFnCall$head$node = jestFnCall.head.node.parent) === null || _jestFnCall$head$node === void 0 ? void 0 : _jestFnCall$head$node.type) === _utils.AST_NODE_TYPES.MemberExpression && jestFnCall.members.length === 1 && !['assertions', 'hasAssertions'].includes((0, _utils2.getAccessorValue)(jestFnCall.members[0]))) {
  66. return;
  67. }
  68. const parent = callStack[callStack.length - 1];
  69. if (!parent || parent === _utils2.DescribeAlias.describe) {
  70. context.report({
  71. node,
  72. messageId: 'unexpectedExpect'
  73. });
  74. }
  75. return;
  76. }
  77. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test' || isCustomTestBlockFunction(node)) {
  78. callStack.push('test');
  79. }
  80. if (node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression) {
  81. callStack.push('template');
  82. }
  83. },
  84. 'CallExpression:exit'(node) {
  85. const top = callStack[callStack.length - 1];
  86. if (top === 'test' && ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test']) || isCustomTestBlockFunction(node)) && node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression || top === 'template' && node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression) {
  87. callStack.pop();
  88. }
  89. },
  90. BlockStatement(statement) {
  91. const blockType = getBlockType(statement, context);
  92. if (blockType) {
  93. callStack.push(blockType);
  94. }
  95. },
  96. 'BlockStatement:exit'(statement) {
  97. if (callStack[callStack.length - 1] === getBlockType(statement, context)) {
  98. callStack.pop();
  99. }
  100. },
  101. ArrowFunctionExpression(node) {
  102. var _node$parent;
  103. if (((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) !== _utils.AST_NODE_TYPES.CallExpression) {
  104. callStack.push('arrow');
  105. }
  106. },
  107. 'ArrowFunctionExpression:exit'() {
  108. if (callStack[callStack.length - 1] === 'arrow') {
  109. callStack.pop();
  110. }
  111. }
  112. };
  113. }
  114. });
  115. exports.default = _default;