prefer-expect-assertions.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. const isExpectAssertionsOrHasAssertionsCall = expression => expression.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && expression.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(expression.callee.object, 'expect') && (0, _utils.isSupportedAccessor)(expression.callee.property) && ['assertions', 'hasAssertions'].includes((0, _utils.getAccessorValue)(expression.callee.property));
  9. const isFirstLineExprStmt = functionBody => functionBody[0] && functionBody[0].type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement;
  10. const suggestRemovingExtraArguments = (args, extraArgsStartAt) => ({
  11. messageId: 'suggestRemovingExtraArguments',
  12. fix: fixer => fixer.removeRange([args[extraArgsStartAt].range[0] - Math.sign(extraArgsStartAt), args[args.length - 1].range[1]])
  13. });
  14. const suggestions = [['suggestAddingHasAssertions', 'expect.hasAssertions();'], ['suggestAddingAssertions', 'expect.assertions();']];
  15. var _default = (0, _utils.createRule)({
  16. name: __filename,
  17. meta: {
  18. docs: {
  19. category: 'Best Practices',
  20. description: 'Suggest using `expect.assertions()` OR `expect.hasAssertions()`',
  21. recommended: false,
  22. suggestion: true
  23. },
  24. messages: {
  25. hasAssertionsTakesNoArguments: '`expect.hasAssertions` expects no arguments',
  26. assertionsRequiresOneArgument: '`expect.assertions` excepts a single argument of type number',
  27. assertionsRequiresNumberArgument: 'This argument should be a number',
  28. haveExpectAssertions: 'Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression',
  29. suggestAddingHasAssertions: 'Add `expect.hasAssertions()`',
  30. suggestAddingAssertions: 'Add `expect.assertions(<number of assertions>)`',
  31. suggestRemovingExtraArguments: 'Remove extra arguments'
  32. },
  33. type: 'suggestion',
  34. schema: [{
  35. type: 'object',
  36. properties: {
  37. onlyFunctionsWithAsyncKeyword: {
  38. type: 'boolean'
  39. }
  40. },
  41. additionalProperties: false
  42. }]
  43. },
  44. defaultOptions: [{
  45. onlyFunctionsWithAsyncKeyword: false
  46. }],
  47. create(context, [options]) {
  48. return {
  49. CallExpression(node) {
  50. if (!(0, _utils.isTestCaseCall)(node)) {
  51. return;
  52. }
  53. if (node.arguments.length < 2) {
  54. return;
  55. }
  56. const [, testFn] = node.arguments;
  57. if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement || options.onlyFunctionsWithAsyncKeyword && !testFn.async) {
  58. return;
  59. }
  60. const testFuncBody = testFn.body.body;
  61. if (!isFirstLineExprStmt(testFuncBody)) {
  62. context.report({
  63. messageId: 'haveExpectAssertions',
  64. node,
  65. suggest: suggestions.map(([messageId, text]) => ({
  66. messageId,
  67. fix: fixer => fixer.insertTextBeforeRange([testFn.body.range[0] + 1, testFn.body.range[1]], text)
  68. }))
  69. });
  70. return;
  71. }
  72. const testFuncFirstLine = testFuncBody[0].expression;
  73. if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
  74. context.report({
  75. messageId: 'haveExpectAssertions',
  76. node,
  77. suggest: suggestions.map(([messageId, text]) => ({
  78. messageId,
  79. fix: fixer => fixer.insertTextBefore(testFuncBody[0], text)
  80. }))
  81. });
  82. return;
  83. }
  84. if ((0, _utils.isSupportedAccessor)(testFuncFirstLine.callee.property, 'hasAssertions')) {
  85. if (testFuncFirstLine.arguments.length) {
  86. context.report({
  87. messageId: 'hasAssertionsTakesNoArguments',
  88. node: testFuncFirstLine.callee.property,
  89. suggest: [suggestRemovingExtraArguments(testFuncFirstLine.arguments, 0)]
  90. });
  91. }
  92. return;
  93. }
  94. if (!(0, _utils.hasOnlyOneArgument)(testFuncFirstLine)) {
  95. let {
  96. loc
  97. } = testFuncFirstLine.callee.property;
  98. const suggest = [];
  99. if (testFuncFirstLine.arguments.length) {
  100. loc = testFuncFirstLine.arguments[1].loc;
  101. suggest.push(suggestRemovingExtraArguments(testFuncFirstLine.arguments, 1));
  102. }
  103. context.report({
  104. messageId: 'assertionsRequiresOneArgument',
  105. suggest,
  106. loc
  107. });
  108. return;
  109. }
  110. const [arg] = testFuncFirstLine.arguments;
  111. if (arg.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof arg.value === 'number' && Number.isInteger(arg.value)) {
  112. return;
  113. }
  114. context.report({
  115. messageId: 'assertionsRequiresNumberArgument',
  116. node: arg
  117. });
  118. }
  119. };
  120. }
  121. });
  122. exports.default = _default;