UNPKG

@mmisty/cypress-grep

Version:

Filters tests by tags/title using substring or regular expressions (can find dynamic tags)

110 lines (109 loc) 6.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerCypressGrep = exports.isInteractive = void 0; const select_tests_1 = require("./select-tests"); const regexp_1 = require("../utils/regexp"); const search_input_1 = require("./search-input"); const cypress_controls_ext_1 = require("cypress-controls-ext"); const envVars_1 = require("../common/envVars"); const logs_1 = require("../common/logs"); const public_config_1 = require("./public-config"); // this controlWrapper- is hardcoded in controls package const wrapperId = (id) => `controlWrapper-${id}`; const isInteractive = () => { // INTER env var for testing return Cypress.config('isInteractive') || (0, envVars_1.isTrue)((0, public_config_1.getPublic)('INTER')); }; exports.isInteractive = isInteractive; const getGrepExpression = (parentId) => { const uiValue = (0, cypress_controls_ext_1.cypressAppSelect)(`#${wrapperId(parentId)} .grep`).val(); // use UI input value only when interactive mode if (!(0, public_config_1.getPublic)('TEST_GREP') && (0, exports.isInteractive)() && uiValue != null) { return `${uiValue}`; } const grep = (0, public_config_1.getPublic)(envVars_1.grepEnvVars.GREP); return grep ? `${grep}` : ''; }; const selectTests = (parentId) => () => { const grepSelected = getGrepExpression(parentId); return (0, regexp_1.selectionTestGrep)(grepSelected); }; const elVal = (selector, dataSelector, initial) => { const el = (0, cypress_controls_ext_1.cypressAppSelect)(selector); if (!(el === null || el === void 0 ? void 0 : el.attr('class'))) { console.log(`${logs_1.pkgName} '${selector}' NOT LOADED YET`); return initial; } return el.attr(dataSelector) === 'true'; }; const logCreate = (config) => (message) => { if (config === null || config === void 0 ? void 0 : config.debugLog) { console.log(`${logs_1.pkgName} ${message}`); } }; const boolOrDefault = (val, res) => { return val === undefined ? res : val === 'true' || val === true; }; const replaceSpecialChars = (str) => { const encodedTitle = str.replace(/([(){}[\]*^.!|$]+)/g, '\\$1'); // replace specific symbols for parsing by grep to any symbol return encodedTitle.replace(/[/'&"]/g, '.'); }; const updateGrepForSpec = () => { var _a, _b; const originalGrep = (0, public_config_1.getPublic)(envVars_1.grepEnvVars.GREP); const filteredSpecsResult = (0, public_config_1.getPublic)('filteredSpecsResult'); if (!filteredSpecsResult) { return; } const spec = Cypress.spec; const tests = (_b = (_a = filteredSpecsResult.tests) === null || _a === void 0 ? void 0 : _a.filter((x) => { var _a; const pathFixed = `${(_a = filteredSpecsResult.parentFolder) !== null && _a !== void 0 ? _a : ''}/${x.filePath}`.replace(/\/\//g, '/'); return spec.relative.includes(pathFixed.startsWith('/') ? pathFixed.slice(1) : pathFixed); }).filter((x) => !!x.title)) !== null && _b !== void 0 ? _b : []; if (tests.length > 0) { const specGrep = tests.map((x) => { var _a; return replaceSpecialChars((_a = x.title) !== null && _a !== void 0 ? _a : ''); }).join('|'); if (specGrep) { (0, public_config_1.setPublic)(envVars_1.grepEnvVars.GREP, `(${originalGrep})${specGrep ? '&' + `(${specGrep})` : ''}`); } } }; // this is being executed at first before any cypress events // and Cypress GREP env var is being read once before filtering tests // So you cannot change it dynamically during spec execution const registerCypressGrep = (configInput) => { var _a, _b, _c; updateGrepForSpec(); const defaultConfig = { addControlToUI: boolOrDefault((0, public_config_1.getPublic)(envVars_1.grepEnvVars.addControlToUI), true), showTagsInTitle: boolOrDefault((0, public_config_1.getPublic)(envVars_1.grepEnvVars.showTagsInTitle), true), showExcludedTests: boolOrDefault((0, public_config_1.getPublic)(envVars_1.grepEnvVars.showExcludedTests), true), }; const config = configInput ? Object.assign(Object.assign({}, defaultConfig), configInput) : defaultConfig; const debug = logCreate(config); const initShowTagsInTitle = (_a = config === null || config === void 0 ? void 0 : config.showTagsInTitle) !== null && _a !== void 0 ? _a : false; const initShowExcludedTests = (_b = config === null || config === void 0 ? void 0 : config.showExcludedTests) !== null && _b !== void 0 ? _b : false; const failExpose = (0, public_config_1.getPublic)(envVars_1.grepEnvVars.failOnNotFound); const envFailNotFound = failExpose != null ? failExpose === 'true' || failExpose === true : undefined; const failOnNotFound = (_c = envFailNotFound !== null && envFailNotFound !== void 0 ? envFailNotFound : config === null || config === void 0 ? void 0 : config.failOnNotFound) !== null && _c !== void 0 ? _c : true; const isPreFilter = (0, envVars_1.isTrue)((0, public_config_1.getPublic)(envVars_1.grepEnvVars.GREP_PRE_FILTER)); console.log(`${logs_1.pkgName} ${failOnNotFound ? 'will fail when no tests found ' : 'will not fail when no tests found'} (to change this set ${envVars_1.grepEnvVars.failOnNotFound} env var to ${!failOnNotFound} )`); console.log(`${logs_1.pkgName} ${envVars_1.grepEnvVars.GREP_PRE_FILTER}: ${isPreFilter}`); // here you can do setup for each test file in browser debug('REGISTER CYPRESS GREP: '); let showTagsInTitle = initShowTagsInTitle; let showExcludedTests = initShowExcludedTests; let idSelector = ''; if (config === null || config === void 0 ? void 0 : config.addControlToUI) { idSelector = (0, search_input_1.addSearchInput)(showTagsInTitle, showExcludedTests); } if ((0, exports.isInteractive)()) { showTagsInTitle = elVal(`#${wrapperId(idSelector)} .show-tags`, 'data-show-tags', initShowTagsInTitle); showExcludedTests = elVal(`#${wrapperId(idSelector)} .show-pending`, 'data-show-pending', initShowExcludedTests); } const configEvaluated = Object.assign(Object.assign({}, config), { showTagsInTitle, showExcludedTests, failOnNotFound }); debug(configEvaluated); (0, select_tests_1.setupSelectTests)(selectTests(idSelector), configEvaluated, (0, search_input_1.updateCount)(wrapperId(idSelector)), isPreFilter); }; exports.registerCypressGrep = registerCypressGrep;