UNPKG

eslint-plugin-playwright

Version:
71 lines (70 loc) 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const misc_1 = require("../utils/misc"); const ast_1 = require("../utils/ast"); exports.default = { create(context) { const { maxTopLevelDescribes } = { maxTopLevelDescribes: Infinity, ...(context.options?.[0] ?? {}), }; let topLevelDescribeCount = 0; let describeCount = 0; return { CallExpression(node) { if ((0, ast_1.isDescribeCall)(node)) { describeCount++; if (describeCount === 1) { topLevelDescribeCount++; if (topLevelDescribeCount > maxTopLevelDescribes) { context.report({ node: node.callee, messageId: 'tooManyDescribes', data: (0, misc_1.getAmountData)(maxTopLevelDescribes), }); } } } else if (!describeCount) { if ((0, ast_1.isTest)(node)) { context.report({ node: node.callee, messageId: 'unexpectedTest' }); } else if ((0, ast_1.isTestHook)(node)) { context.report({ node: node.callee, messageId: 'unexpectedHook' }); } } }, 'CallExpression:exit'(node) { if ((0, ast_1.isDescribeCall)(node)) { describeCount--; } }, }; }, meta: { docs: { category: 'Best Practices', description: 'Require test cases and hooks to be inside a `test.describe` block', recommended: false, url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-top-level-describe.md', }, messages: { tooManyDescribes: 'There should not be more than {{max}} describe{{s}} at the top level', unexpectedTest: 'All test cases must be wrapped in a describe block.', unexpectedHook: 'All hooks must be wrapped in a describe block.', }, type: 'suggestion', schema: [ { type: 'object', properties: { maxTopLevelDescribes: { type: 'number', minimum: 1, }, }, additionalProperties: false, }, ], }, };