UNPKG

@mmisty/cypress-grep

Version:

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

69 lines (67 loc) 2.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createOneTestsFile = exports.createAllTestsFile = void 0; const fast_glob_1 = __importDefault(require("fast-glob")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const logs_1 = require("../common/logs"); const parentSuite = (tests) => { return `describe('', () => { let error = []; ${tests} if(error.length > 0) { throw new Error('${logs_1.pkgName} Prefiltering failed as some of your test files crashed outside of test:\\n ' + error.map((t,i)=>(i + 1) + ') ' + t.message).join('\\n\\n ')); } }); `; }; const testCode = (relativePath) => { return ` describe(\`\$\{__dirname\}${relativePath.replace(/\/\/+/g, '/')}\`, () => { try { require('.${relativePath}'); } catch(e){ e.message = \`${logs_1.pkgName} Prefiltering cannot be done for \\'\$\{__dirname\}${relativePath}\\', error when executing file: \\n > \$\{e.message\}\`; error.push(e); } });`; }; const testAutoGreneratedCode = () => { return `it('auto generated test when no GREP set', () => { // ignore });`; }; /** * Will create file with all tests imported with require * @param outFilePath * @param testsDir - directory where file will be created, should be in tests * @param specPattern */ const createAllTestsFile = (outFilePath, testsDir, specPattern) => { const code = fast_glob_1.default .sync(specPattern) .map(r => { // for 'require' to correctly resole need relative path (relative to path of created file outFilePath) const relative = r.slice(r.indexOf(testsDir) + testsDir.length); const pathMatch = new RegExp(path_1.default.basename(outFilePath).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')); if (!pathMatch.test(relative)) { return testCode(relative); } return undefined; }) .filter(t => t); fs_1.default.writeFileSync(outFilePath, parentSuite(code.join('\n\n'))); // eslint-disable-next-line no-console console.log(`${logs_1.pkgName} Created file with all tests '${outFilePath}'`); return outFilePath; }; exports.createAllTestsFile = createAllTestsFile; const createOneTestsFile = (outFilePath) => { fs_1.default.writeFileSync(outFilePath, parentSuite(testAutoGreneratedCode())); // eslint-disable-next-line no-console console.log(`${logs_1.pkgName} Created file with one test '${outFilePath}'`); return outFilePath; }; exports.createOneTestsFile = createOneTestsFile;