Skip to content

Commit

Permalink
[minor] added parallel runs support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty authored Sep 29, 2023
1 parent 037fec9 commit 38bbb75
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
/integration/screenshots

# prefilter
all-tests.ts
filtered_test_paths.json
all-tests*.ts
filtered_test_paths*.json
filtered_tests.json
filtered.json
spec_pattern.json
filtered*.json
spec_pattern*.json

# coverage and test reports
/reports
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
## Change Log
### 1.8.0
- support for parallel runs on one machine

### 1.7.1
- added license field

### 1.7.0
- yarn support

### 1.6.10
- [cy-grep] yarn support

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test:cov": "CI=true jest --testPathIgnorePatterns=\"tests/test-folder/integration\" --coverage --runInBand --coverageDirectory reports/coverage-jest --reporters=default --reporters=jest-junit",
"cy:open": "npx cypress open --browser chrome --e2e",
"cy:run:grep": "node src/bin/cy-grep.js --script 'npm run cy:run' --p './reports/filtered.json'",
"cy:open:all": "npm run cy:open -- --browser chrome --config specPattern='integration/**/all-tests.ts'",
"cy:open:all": "npm run cy:open -- --browser chrome --config specPattern='integration/**/all-tests.js'",
"cy:run": "npx cypress run --browser chrome --headless",
"cy:run:regress": "npm run cy:run -- --config specPattern='integration/e2e/regression/**/*.ts'",
"cy:run:grep:cov": "CYPRESS_COVERAGE=true COVERAGE_REPORT_DIR=reports/coverage-cypress npm run cy:run:grep",
Expand All @@ -40,12 +40,16 @@
"cy:run:2": "CYPRESS_GREP_showTagsInTitle=false npm run cy:run:grep:cov",
"cy:run:3": "CYPRESS_INTER=true npm run cy:run:grep:cov -- -- --script 'npm run cy:run -- --config specPattern=\"integration/e2e/**/*.cy.ts\"' ",
"cy:run:4": "CYPRESS_GREP='@cyApi&!@navbar' CYPRESS_GREP_showTagsInTitle=true npm run cy:run:grep:cov",
"cy:run:4.1": "CYPRESS_GREP='@cyApi&' npm run cy:run:grep:cov",
"cy:run:4.2": "CYPRESS_GREP='@navbar' npm run cy:run:grep:cov",
"cy:run:4.3": "CYPRESS_GREP='@navbar' npm run cy:run:grep:cov",
"cy:run:parallel": "npm run cy:run:4.1 & npm run cy:run:4.2 & npm run cy:run:4.3",
"cy:run:5": "CYPRESS_GREP='@cyApi&!@navbar' CYPRESS_GREP_showTagsInTitle=false npm run cy:run:grep:cov",
"cy:run:6": "CYPRESS_GREP='@cyApi&!@navbar' CYPRESS_GREP_showTagsInTitle=false CYPRESS_GREP_showExcludedTests=false npm run cy:run:grep:cov",
"cy:run:7": "CYPRESS_GREP_TESTS_FOLDER='integration/e2e' CYPRESS_GREP='@navbar' CYPRESS_GREP_showTagsInTitle=false npm run cy:run:grep:cov",
"cy:run:8": "CYPRESS_GREP_TESTS_FOLDER='integration/e2e' CYPRESS_GREP='@notExisting' npm run cy:run:grep:cov || true",
"cy:run:9": "npm run cy:run:grep:cov -- -- --f",
"cy:run:cov": "npm run cy:run:1 && npm run cy:run:2 && npm run cy:run:3 && npm run cy:run:4 && npm run cy:run:5 && npm run cy:run:6&& npm run cy:run:7&& npm run cy:run:8&& npm run cy:run:9 ",
"cy:run:cov": "npm run cy:run:1 && npm run cy:run:2 && npm run cy:run:3 && npm run cy:run:4 && npm run cy:run:5 && npm run cy:run:6&& npm run cy:run:7&& npm run cy:run:8&& npm run cy:run:9 && npm run cy:run:parallel",
"cy:open:cov": "COVERAGE_REPORT_DIR=reports/coverage-cypress COVERAGE=true npm run cy:open",
"build": "rm -rf lib && tsc --project tsconfig.build.json",
"build:tests": "tsc && cd tests && tsc",
Expand Down
36 changes: 33 additions & 3 deletions src/bin/cy-grep.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { existsSync, rmSync, readFileSync } = require('fs');
const yargs = require('yargs');

const packagename = '[cypress-grep]';
const fileSpecPatternOriginal = 'spec_pattern.json';

const argv = yargs(process.argv.slice(2))
.options({
Expand All @@ -31,6 +30,12 @@ const argv = yargs(process.argv.slice(2))
describe: 'file where prefiltered results will be stored',
alias: 'p',
},
session: {
type: 'string',
default: 400000 + Math.round(Math.random() * 250000),
describe: 'seesion number (to not have conflicts with parallel runs), default random number',
alias: 'ss',
},
'only-prefilter': {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -73,19 +78,28 @@ const {
script: scriptInput,
grep: grepInput,
prefilterFile: prefilterFileInput,
session: randomSession,
deletePrefiltered,
onlyPrefilter,
onlyRun,
showExcludedTests,
showTagsTitle,
} = argv;

const fileSpecPatternOriginal = `spec_pattern${randomSession}.json`;

let grep = !grepInput && process.env.CYPRESS_GREP ? process.env.CYPRESS_GREP : grepInput;
let script = Array.isArray(scriptInput) ? scriptInput[scriptInput.length - 1] : scriptInput;
let prefilterFile = Array.isArray(prefilterFileInput)
? prefilterFileInput[prefilterFileInput.length - 1]
: prefilterFileInput;

if (prefilterFile.indexOf(randomSession) === -1) {
const extPos = `${prefilterFile}`.lastIndexOf('.');
const ext = prefilterFile.slice(extPos);
prefilterFile = `${prefilterFile.slice(0, extPos)}${randomSession}${ext}`;
}

const getGrepEnvVariableStr = grepInputT => {
if (grepInputT) {
return `CYPRESS_GREP='${grepInputT}'`;
Expand Down Expand Up @@ -179,7 +193,15 @@ try {
}
} else {
console.log(`${packagename} PRE-FILTERING MODE ${onlyPrefilter ? 'only prefilter' : ''}=== `);
execute([grepExpression, resultsFileEnvVariableStr, 'CYPRESS_GREP_PRE_FILTER=true'], script);
execute(
[
grepExpression,
resultsFileEnvVariableStr,
'CYPRESS_GREP_PRE_FILTER=true',
`CYPRESS_GREP_SESSION=${randomSession}`,
],
script,
);
const prefilteringDuration = `${(Date.now() - started) / 1000}s`;

if (onlyPrefilter) {
Expand All @@ -203,7 +225,15 @@ try {
const showTags = showTagsTitle !== undefined ? `CYPRESS_GREP_showTagsInTitle=${showTagsTitle === true}` : '';

execute(
[grepExpression, resultsFileEnvVariableStr, 'CYPRESS_GREP_PRE_FILTER=false', specString, exclTests, showTags],
[
grepExpression,
resultsFileEnvVariableStr,
'CYPRESS_GREP_PRE_FILTER=false',
`CYPRESS_GREP_SESSION=${randomSession}`,
specString,
exclTests,
showTags,
],
script,
);

Expand Down
5 changes: 5 additions & 0 deletions src/common/envVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export enum grepEnvVars {
*/
'GREP' = 'GREP',

/**
* Session number to not have conflicts with all_tests files and filtered results
*/
'GREP_SESSION' = 'GREP_SESSION',

/**
* Whether to fail run when no tests are found, default true
*/
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ export const pluginGrep = (on: Cypress.PluginEvents, config: Cypress.PluginConfi
const isPreFilter = isTrue(config.env[grepEnvVars.GREP_PRE_FILTER] ?? false);
const isDeleteAllFile = isTrue(config.env[grepEnvVars.GREP_DELETE_ALL_FILE] ?? true);
const grep = config.env[grepEnvVars.GREP];
const filteredSpecs = config.env[grepEnvVars.GREP_RESULTS_FILE] ?? `${config.projectRoot}/filtered_test_paths.json`;
const allFileName = config.env[grepEnvVars.GREP_ALL_TESTS_NAME] ?? 'all-tests.js';
const randomSession = config.env[grepEnvVars.GREP_SESSION];

const filteredSpecs =
config.env[grepEnvVars.GREP_RESULTS_FILE] ?? `${config.projectRoot}/filtered_test_paths${randomSession}.json`;
const allFileName = config.env[grepEnvVars.GREP_ALL_TESTS_NAME] ?? `all-tests${randomSession}.js`;
const allTestsFile = `${parentTestsFolder}/${allFileName}`;
on('task', taskWrite(config, parentTestsFolder, filteredSpecs));

console.log(`${pkgName} grep: ${grep ?? 'not set'}`);
console.log(`${pkgName} parent tests folder: '${parentTestsFolder}'`);
console.log(`${pkgName} grep: session number ${randomSession}`);

if (!existsSync(dirname(filteredSpecs))) {
mkdirSync(dirname(filteredSpecs), { recursive: true });
Expand All @@ -108,7 +112,7 @@ export const pluginGrep = (on: Cypress.PluginEvents, config: Cypress.PluginConfi

return;
}
writeFileSync('spec_pattern.json', JSON.stringify({ specPattern: specPattern }));
writeFileSync(`spec_pattern${randomSession}.json`, JSON.stringify({ specPattern: specPattern }));
config.reporter = 'spec';
config.video = false;
config.env['REDIRECT_BROWSER_LOG'] = false;
Expand Down
4 changes: 3 additions & 1 deletion tests/test-folder/integration/many-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import expect from 'expect';
import { createTests, deleteResults, resSorted, runTests } from '../../utils/helper';
import { readFileSync } from 'fs';
import glob from 'fast-glob';

describe('many tests', () => {
beforeEach(() => {
Expand Down Expand Up @@ -39,7 +40,8 @@ describe('many tests', () => {
'--prefilter-file ./reports/res.json',
]);
const durationSec = (Date.now() - started) / 1000;
const res = JSON.parse(readFileSync('./reports/res.json').toString()).tests.length;
const files = glob.sync('./reports/res*.json');
const res = JSON.parse(readFileSync(files[0]).toString()).tests.length;
expect(res).toEqual(2001);
expect(durationSec).toBeLessThan(100);
});
Expand Down

0 comments on commit 38bbb75

Please sign in to comment.