prefer-expect-assertions.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 isFirstStatement = node => {
  9. let parent = node;
  10. while (parent) {
  11. var _parent$parent;
  12. if (((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === _utils.AST_NODE_TYPES.BlockStatement) {
  13. return parent.parent.body[0] === parent;
  14. }
  15. parent = parent.parent;
  16. }
  17. /* istanbul ignore next */
  18. throw new Error(`Could not find BlockStatement - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
  19. };
  20. const suggestRemovingExtraArguments = (args, extraArgsStartAt) => ({
  21. messageId: 'suggestRemovingExtraArguments',
  22. fix: fixer => fixer.removeRange([args[extraArgsStartAt].range[0] - Math.sign(extraArgsStartAt), args[args.length - 1].range[1]])
  23. });
  24. // const suggestions: Array<[MessageIds, string]> = [
  25. // ['suggestAddingHasAssertions', 'expect.hasAssertions();'],
  26. // ['suggestAddingAssertions', 'expect.assertions();'],
  27. // ];
  28. var _default = (0, _utils2.createRule)({
  29. name: __filename,
  30. meta: {
  31. docs: {
  32. category: 'Best Practices',
  33. description: 'Suggest using `expect.assertions()` OR `expect.hasAssertions()`',
  34. recommended: false,
  35. suggestion: true
  36. },
  37. messages: {
  38. hasAssertionsTakesNoArguments: '`expect.hasAssertions` expects no arguments',
  39. assertionsRequiresOneArgument: '`expect.assertions` excepts a single argument of type number',
  40. assertionsRequiresNumberArgument: 'This argument should be a number',
  41. haveExpectAssertions: 'Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression',
  42. suggestAddingHasAssertions: 'Add `expect.hasAssertions()`',
  43. suggestAddingAssertions: 'Add `expect.assertions(<number of assertions>)`',
  44. suggestRemovingExtraArguments: 'Remove extra arguments'
  45. },
  46. type: 'suggestion',
  47. hasSuggestions: true,
  48. schema: [{
  49. type: 'object',
  50. properties: {
  51. onlyFunctionsWithAsyncKeyword: {
  52. type: 'boolean'
  53. },
  54. onlyFunctionsWithExpectInLoop: {
  55. type: 'boolean'
  56. },
  57. onlyFunctionsWithExpectInCallback: {
  58. type: 'boolean'
  59. }
  60. },
  61. additionalProperties: false
  62. }]
  63. },
  64. defaultOptions: [{
  65. onlyFunctionsWithAsyncKeyword: false,
  66. onlyFunctionsWithExpectInLoop: false,
  67. onlyFunctionsWithExpectInCallback: false
  68. }],
  69. create(context, [options]) {
  70. let expressionDepth = 0;
  71. let hasExpectInCallback = false;
  72. let hasExpectInLoop = false;
  73. let hasExpectAssertionsAsFirstStatement = false;
  74. let inTestCaseCall = false;
  75. let inForLoop = false;
  76. const shouldCheckFunction = testFunction => {
  77. if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop && !options.onlyFunctionsWithExpectInCallback) {
  78. return true;
  79. }
  80. if (options.onlyFunctionsWithAsyncKeyword) {
  81. if (testFunction.async) {
  82. return true;
  83. }
  84. }
  85. if (options.onlyFunctionsWithExpectInLoop) {
  86. if (hasExpectInLoop) {
  87. return true;
  88. }
  89. }
  90. if (options.onlyFunctionsWithExpectInCallback) {
  91. if (hasExpectInCallback) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. };
  97. const checkExpectHasAssertions = expectFnCall => {
  98. if ((0, _utils2.getAccessorValue)(expectFnCall.members[0]) === 'hasAssertions') {
  99. if (expectFnCall.args.length) {
  100. context.report({
  101. messageId: 'hasAssertionsTakesNoArguments',
  102. node: expectFnCall.matcher,
  103. suggest: [suggestRemovingExtraArguments(expectFnCall.args, 0)]
  104. });
  105. }
  106. return;
  107. }
  108. if (expectFnCall.args.length !== 1) {
  109. let {
  110. loc
  111. } = expectFnCall.matcher;
  112. const suggest = [];
  113. if (expectFnCall.args.length) {
  114. loc = expectFnCall.args[1].loc;
  115. suggest.push(suggestRemovingExtraArguments(expectFnCall.args, 1));
  116. }
  117. context.report({
  118. messageId: 'assertionsRequiresOneArgument',
  119. suggest,
  120. loc
  121. });
  122. return;
  123. }
  124. const [arg] = expectFnCall.args;
  125. if (arg.type === _utils.AST_NODE_TYPES.Literal && typeof arg.value === 'number' && Number.isInteger(arg.value)) {
  126. return;
  127. }
  128. context.report({
  129. messageId: 'assertionsRequiresNumberArgument',
  130. node: arg
  131. });
  132. };
  133. const enterExpression = () => inTestCaseCall && expressionDepth++;
  134. const exitExpression = () => inTestCaseCall && expressionDepth--;
  135. const enterForLoop = () => inForLoop = true;
  136. const exitForLoop = () => inForLoop = false;
  137. return {
  138. FunctionExpression: enterExpression,
  139. 'FunctionExpression:exit': exitExpression,
  140. ArrowFunctionExpression: enterExpression,
  141. 'ArrowFunctionExpression:exit': exitExpression,
  142. ForStatement: enterForLoop,
  143. 'ForStatement:exit': exitForLoop,
  144. ForInStatement: enterForLoop,
  145. 'ForInStatement:exit': exitForLoop,
  146. ForOfStatement: enterForLoop,
  147. 'ForOfStatement:exit': exitForLoop,
  148. CallExpression(node) {
  149. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  150. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test') {
  151. inTestCaseCall = true;
  152. return;
  153. }
  154. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'expect' && inTestCaseCall) {
  155. var _jestFnCall$head$node;
  156. if (expressionDepth === 1 && isFirstStatement(node) && ((_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]))) {
  157. checkExpectHasAssertions(jestFnCall);
  158. hasExpectAssertionsAsFirstStatement = true;
  159. }
  160. if (inForLoop) {
  161. hasExpectInLoop = true;
  162. }
  163. if (expressionDepth > 1) {
  164. hasExpectInCallback = true;
  165. }
  166. }
  167. },
  168. 'CallExpression:exit'(node) {
  169. if (!(0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
  170. return;
  171. }
  172. inTestCaseCall = false;
  173. if (node.arguments.length < 2) {
  174. return;
  175. }
  176. const [, testFn] = node.arguments;
  177. if (!(0, _utils2.isFunction)(testFn) || !shouldCheckFunction(testFn)) {
  178. return;
  179. }
  180. hasExpectInLoop = false;
  181. hasExpectInCallback = false;
  182. if (hasExpectAssertionsAsFirstStatement) {
  183. hasExpectAssertionsAsFirstStatement = false;
  184. return;
  185. }
  186. const suggestions = [];
  187. if (testFn.body.type === _utils.AST_NODE_TYPES.BlockStatement) {
  188. suggestions.push(['suggestAddingHasAssertions', 'expect.hasAssertions();'], ['suggestAddingAssertions', 'expect.assertions();']);
  189. }
  190. context.report({
  191. messageId: 'haveExpectAssertions',
  192. node,
  193. suggest: suggestions.map(([messageId, text]) => ({
  194. messageId,
  195. fix: fixer => fixer.insertTextBeforeRange([testFn.body.range[0] + 1, testFn.body.range[1]], text)
  196. }))
  197. });
  198. }
  199. };
  200. }
  201. });
  202. exports.default = _default;