UNPKG

eslint-plugin-sonarjs

Version:
65 lines (64 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTestConstruct = isTestConstruct; exports.extractTestCase = extractTestCase; exports.isTestCase = isTestCase; exports.isDescribeCase = isDescribeCase; exports.isLifecycleHook = isLifecycleHook; const ast_js_1 = require("./ast.js"); const LIFECYCLE_HOOK_CONSTRUCTS = [ 'before', 'after', 'beforeAll', 'afterAll', 'beforeEach', 'afterEach', ]; const TEST_CONSTRUCTS = ['describe', 'context', 'it', 'specify', ...LIFECYCLE_HOOK_CONSTRUCTS]; const TEST_CASE_CONSTRUCTS = ['it', 'specify', 'test']; function isTestConstruct(node, constructs = TEST_CONSTRUCTS) { return constructs.some(construct => { return (node.type === 'CallExpression' && ((0, ast_js_1.isIdentifier)(node.callee, construct) || (node.callee.type === 'MemberExpression' && (0, ast_js_1.isIdentifier)(node.callee.object, construct) && (0, ast_js_1.isIdentifier)(node.callee.property, 'only', 'skip')))); }); } function extractTestCase(node) { if (isTestCase(node)) { const callExpr = node; const [, callback] = callExpr.arguments; if (callback && ast_js_1.FUNCTION_NODES.includes(callback.type)) { return { node: callExpr.callee, callback: callback }; } } return null; } /** * returns true if the node is a test case * * @param node * @returns */ function isTestCase(node) { return isTestConstruct(node, TEST_CASE_CONSTRUCTS); } /** * returns true if the node is a describe block * * @param node * @returns */ function isDescribeCase(node) { return isTestConstruct(node, ['describe', 'context']); } /** * returns true if the node is a lifecycle hook * * @param node * @returns */ function isLifecycleHook(node) { return isTestConstruct(node, LIFECYCLE_HOOK_CONSTRUCTS); }