UNPKG

@studyportals/cypress-slicer

Version:

Cypress plugin to slice the specPattern list of tests into subsets

67 lines (51 loc) 1.73 kB
const glob = require("glob"); const logger = require("./lib/logger") /** * Selects only a subset of the files in the glob pattern. * @param {Cypress.ConfigOptions} config */ function cypressSlicerPlugin(config) { if (!config || !config.env) { return config; } if (!config.specPattern) { throw new Error( "Incompatible Cypress version detected; requires Cypress 10.0.0+" ); } const { env } = config; const workers = Number(env.workers) || 1; const worker = Number(env.worker) || 1; if (worker >= 1 && worker <= workers) { if (workers > 1) { logger.log(`cypress-slicer: running worker ${worker} of ${workers}`); } } else { throw new Error( `cypress-slicer: invalid worker number ${worker} of ${workers}` ); } const { specPattern, excludeSpecPattern } = config; const specFiles = [...specPattern].reduce((acc, pattern) => { const files = glob.sync(pattern, { ignore: excludeSpecPattern }); return [...acc, ...files]; }, []); const testCounter = specFiles.length; logger.log(`cypress-slicer: found a total of ${testCounter} spec files`); const chunks = (a, size) => { a = a.sort(); return Array.from(new Array(Math.ceil(a.length / size)), (_, i) => a.slice(i * size, i * size + size) ); }; let noTests = Math.ceil(testCounter / workers); const filteredSpecFiles = chunks(specFiles, noTests)[worker - 1]; logger.log(`cypress-slicer: running ${filteredSpecFiles.length} spec files`); if (filteredSpecFiles.length) { config.specPattern = filteredSpecFiles; } else { throw new Error("cypress-slicer: Could not select any spec files"); } return config; } module.exports = cypressSlicerPlugin;