@mmisty/cypress-grep
Version:
Filters tests by tags/title using substring or regular expressions (can find dynamic tags)
80 lines (79 loc) • 4.73 kB
JavaScript
;
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");
// 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)(Cypress.env('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 (!Cypress.env('TEST_GREP') && (0, exports.isInteractive)() && uiValue != null) {
return `${uiValue}`;
}
return Cypress.env(envVars_1.grepEnvVars.GREP) ? `${Cypress.env(envVars_1.grepEnvVars.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 registerCypressGrep = (configInput) => {
var _a, _b, _c;
const defaultConfig = {
addControlToUI: boolOrDefault(Cypress.env(envVars_1.grepEnvVars.addControlToUI), true),
showTagsInTitle: boolOrDefault(Cypress.env(envVars_1.grepEnvVars.showTagsInTitle), true),
showExcludedTests: boolOrDefault(Cypress.env(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 envFailNotFound = Cypress.env(envVars_1.grepEnvVars.failOnNotFound) != null
? Cypress.env(envVars_1.grepEnvVars.failOnNotFound) === 'true' || Cypress.env(envVars_1.grepEnvVars.failOnNotFound) === 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)(Cypress.env(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;