expect-expect.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /*
  9. * This implementation is adapted from eslint-plugin-jasmine.
  10. * MIT license, Remco Haszing.
  11. */
  12. /**
  13. * Checks if node names returned by getNodeName matches any of the given star patterns
  14. * Pattern examples:
  15. * request.*.expect
  16. * request.**.expect
  17. * request.**.expect*
  18. */
  19. function matchesAssertFunctionName(nodeName, patterns) {
  20. return patterns.some(p => new RegExp(`^${p.split('.').map(x => {
  21. if (x === '**') return '[a-z\\.]*';
  22. return x.replace(/\*/gu, '[a-z]*');
  23. }).join('\\.')}(\\.|$)`, 'ui').test(nodeName));
  24. }
  25. var _default = (0, _utils.createRule)({
  26. name: __filename,
  27. meta: {
  28. docs: {
  29. category: 'Best Practices',
  30. description: 'Enforce assertion to be made in a test body',
  31. recommended: 'warn'
  32. },
  33. messages: {
  34. noAssertions: 'Test has no assertions'
  35. },
  36. schema: [{
  37. type: 'object',
  38. properties: {
  39. assertFunctionNames: {
  40. type: 'array',
  41. items: [{
  42. type: 'string'
  43. }]
  44. },
  45. additionalTestBlockFunctions: {
  46. type: 'array',
  47. items: {
  48. type: 'string'
  49. }
  50. }
  51. },
  52. additionalProperties: false
  53. }],
  54. type: 'suggestion'
  55. },
  56. defaultOptions: [{
  57. assertFunctionNames: ['expect'],
  58. additionalTestBlockFunctions: []
  59. }],
  60. create(context, [{
  61. assertFunctionNames = ['expect'],
  62. additionalTestBlockFunctions = []
  63. }]) {
  64. const unchecked = [];
  65. function checkCallExpressionUsed(nodes) {
  66. for (const node of nodes) {
  67. const index = node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? unchecked.indexOf(node) : -1;
  68. if (node.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
  69. const declaredVariables = context.getDeclaredVariables(node);
  70. const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
  71. checkCallExpressionUsed(testCallExpressions);
  72. }
  73. if (index !== -1) {
  74. unchecked.splice(index, 1);
  75. break;
  76. }
  77. }
  78. }
  79. return {
  80. CallExpression(node) {
  81. var _getNodeName;
  82. const name = (_getNodeName = (0, _utils.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 ? _getNodeName : '';
  83. if ((0, _utils.isTestCaseCall)(node) || additionalTestBlockFunctions.includes(name)) {
  84. if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'todo')) {
  85. return;
  86. }
  87. unchecked.push(node);
  88. } else if (matchesAssertFunctionName(name, assertFunctionNames)) {
  89. // Return early in case of nested `it` statements.
  90. checkCallExpressionUsed(context.getAncestors());
  91. }
  92. },
  93. 'Program:exit'() {
  94. unchecked.forEach(node => context.report({
  95. messageId: 'noAssertions',
  96. node
  97. }));
  98. }
  99. };
  100. }
  101. });
  102. exports.default = _default;