expect-expect.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. },
  46. additionalProperties: false
  47. }],
  48. type: 'suggestion'
  49. },
  50. defaultOptions: [{
  51. assertFunctionNames: ['expect']
  52. }],
  53. create(context, [{
  54. assertFunctionNames = ['expect']
  55. }]) {
  56. const unchecked = [];
  57. function checkCallExpressionUsed(nodes) {
  58. for (const node of nodes) {
  59. const index = node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? unchecked.indexOf(node) : -1;
  60. if (node.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
  61. const declaredVariables = context.getDeclaredVariables(node);
  62. const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
  63. checkCallExpressionUsed(testCallExpressions);
  64. }
  65. if (index !== -1) {
  66. unchecked.splice(index, 1);
  67. break;
  68. }
  69. }
  70. }
  71. return {
  72. CallExpression(node) {
  73. const name = (0, _utils.getNodeName)(node.callee);
  74. if (name === _utils.TestCaseName.it || name === _utils.TestCaseName.test) {
  75. unchecked.push(node);
  76. } else if (name && matchesAssertFunctionName(name, assertFunctionNames)) {
  77. // Return early in case of nested `it` statements.
  78. checkCallExpressionUsed(context.getAncestors());
  79. }
  80. },
  81. 'Program:exit'() {
  82. unchecked.forEach(node => context.report({
  83. messageId: 'noAssertions',
  84. node
  85. }));
  86. }
  87. };
  88. }
  89. });
  90. exports.default = _default;