UNPKG

eslint-plugin-sonarjs

Version:
87 lines (86 loc) 3.09 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) 2011-2025 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ // https://sonarsource.github.io/rspec/#/rspec/S5973/javascript Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const index_js_1 = require("../helpers/index.js"); const meta_js_1 = require("./meta.js"); exports.rule = { meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { messages: { stable: 'Make your tests stable so that they pass on the first try, or remove the flaky ones.', }, }), create(context) { const describes = []; const hasJest = hasJestDependency(context); return { /** * We use the stack approach to check for Mocha retries inside describe blocks, * and implicitly test cases. */ CallExpression(node) { if (hasJestRetry(context, node, hasJest)) { report(context, node); return; } if (index_js_1.Mocha.isDescribeCase(node)) { describes.push(node); return; } if (describes.length > 0) { checkMochaRetries(context, node); } }, 'CallExpression:exit': (node) => { if (index_js_1.Mocha.isDescribeCase(node)) { describes.pop(); } }, 'Program:exit': () => { describes.length = 0; }, }; }, }; function hasJestRetry(context, node, hasJest) { const callExpressionName = (0, index_js_1.getFullyQualifiedName)(context, node); return (callExpressionName === 'jest.retryTimes' || (hasJest && (0, index_js_1.isMethodInvocation)(node, 'jest', 'retryTimes', 1))); } function hasJestDependency(context) { const dependencies = (0, index_js_1.getDependencies)(context.filename, context.cwd); return dependencies.has('jest'); } /** * Flag if `this.retries()` */ function checkMochaRetries(context, node) { const callee = node.callee; if (callee.type === 'MemberExpression' && callee.object.type === 'ThisExpression' && (0, index_js_1.isIdentifier)(callee.property, 'retries')) { report(context, node); } } function report(context, node) { context.report({ messageId: 'stable', node, }); }