New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallelize cypress testing #3460
Changes from all commits
57d25e0
58514b9
77de063
1415171
5e35614
f3eafe8
91b6675
5b1ae3b
923aa23
5f5cbed
3e1d113
ef23f43
2e34d2e
01df061
0d9fe0e
5483110
c9eff7f
50c8468
babca09
d481cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,21 @@ | |
"scripts": { | ||
"clearCache:mac": "rm -rf ~/Library/Application\\ Support/Cypress/cy/*", | ||
"test": "cypress open --browser chrome --config-file cypress.config.json", | ||
"test:ci": "./run-tests", | ||
"test:ci": "./run-tests.js -t 4", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this feels a little static to me, to always set it as 4. different machines will have different capacity based on the system cpu, so I think this should be calculated based on the machine. using os.cpus you can get information about the machine and then based on that run threads. Also, it seems like maybe we always want to to run threads and maybe two commands isn't necessary? |
||
"test:debug": "node --inspect-brk ./node_modules/cypress/bin/cypress run --browser chrome --config-file cypress.config.json", | ||
"test:devTest": "jest", | ||
"test:firefox": "cypress open --browser firefox --config-file cypress.config.json", | ||
"test:headless": "./run-tests", | ||
"test:headless": "./run-tests.js", | ||
"test:run": "cypress run --browser chrome --config-file cypress.config.json" | ||
}, | ||
"author": "Magento Commerce", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"cypress": "~7.1.0", | ||
"cypress-image-snapshot": "~4.0.1", | ||
"glob": "~7.2.0", | ||
"jest": "~26.0.1", | ||
"jimp": "~0.16.1" | ||
"jimp": "~0.16.1", | ||
"yargs": "~17.2.1" | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/usr/bin/env node | ||
const glob = require('glob'); | ||
const { exec } = require('child_process'); | ||
|
||
var argv = require('yargs/yargs')(process.argv.slice(2)) | ||
.option('url', { | ||
alias: 'baseUrl', | ||
demandOption: true, | ||
describe: 'Storefront application URL to run tests against', | ||
type: 'string', | ||
nargs: 1 | ||
}) | ||
.option('t', { | ||
alias: 'threads', | ||
default: 1, | ||
describe: 'Number of parallel runs', | ||
type: 'number', | ||
nargs: 1 | ||
}) | ||
.option('u', { | ||
alias: 'update', | ||
default: false, | ||
describe: 'Update snapshots', | ||
type: 'boolean' | ||
}) | ||
.option('s', { | ||
alias: 'spec', | ||
default: null, | ||
describe: 'Spec files to run', | ||
type: 'string', | ||
nargs: 1 | ||
}) | ||
.option('h', { | ||
alias: 'help', | ||
describe: 'Show run-tests help' | ||
}) | ||
.help('h', 'Show run-tests help') | ||
.version(false).argv; | ||
|
||
const { baseUrl, threads, update, spec } = argv; | ||
|
||
if (!baseUrl) { | ||
console.error( | ||
'Missing baseUrl. Please provide a baseUrl using the --baseUrl arg' | ||
); | ||
|
||
return; | ||
} | ||
|
||
const files = spec ? spec.split(',') : glob.sync('./src/tests/**/*.spec.js'); | ||
|
||
if (files.length < threads) { | ||
console.error('Can not have more parallel runs than tests.'); | ||
|
||
return; | ||
} | ||
|
||
const testsPerRun = files.length / threads; | ||
const dockerRuns = {}; | ||
|
||
const port = new URL(baseUrl).port; | ||
|
||
let dockerCommand = null; | ||
|
||
if (port) { | ||
// run docker on local instance | ||
console.log(`Running tests on local instance ${baseUrl}`); | ||
|
||
dockerCommand = `docker run --rm -v ${ | ||
process.env.PWD | ||
}:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=https://host.docker.internal:${port},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless`; | ||
revanth0212 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// run docker on remote instance | ||
console.log(`Running tests on remote instance ${baseUrl}`); | ||
|
||
dockerCommand = `docker run --rm -v ${ | ||
process.env.PWD | ||
}:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=${baseUrl},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless`; | ||
} | ||
|
||
const start = process.hrtime(); | ||
|
||
for (let i = 0; i < threads; i++) { | ||
const filesToTest = files.slice(testsPerRun * i, testsPerRun * (i + 1)); | ||
|
||
const commandWithSpecFiles = `${dockerCommand} --spec ${filesToTest.join( | ||
',' | ||
)}`; | ||
|
||
console.log(`Running ${commandWithSpecFiles} \n`); | ||
|
||
const run = exec(commandWithSpecFiles); | ||
|
||
run.stdout.on('data', data => { | ||
if (data !== '\n' || data !== '\r' || data.trim() !== '') { | ||
console.log(`docker run ${i + 1} => ${data}`); | ||
} | ||
}); | ||
|
||
run.stderr.on('data', data => { | ||
console.error(`docker run ${i + 1} => ${data}`); | ||
}); | ||
|
||
run.on('close', code => { | ||
dockerRuns[i].completed = true; | ||
|
||
const timeTaken = process.hrtime(dockerRuns[i].started)[0]; | ||
|
||
console.log( | ||
`docker run ${i + | ||
1} exited with ${code} code in ${timeTaken} seconds` | ||
); | ||
|
||
if (Object.values(dockerRuns).every(r => r.completed)) { | ||
const totalTime = process.hrtime(start)[0]; | ||
|
||
console.log(`\nAll runs completed in ${totalTime} seconds\n`); | ||
|
||
process.exit(); | ||
} | ||
}); | ||
|
||
dockerRuns[i] = { | ||
process: run, | ||
completed: false, | ||
started: process.hrtime() | ||
}; | ||
} | ||
|
||
process.on('SIGINT', function() { | ||
console.log('Received kill signal. Killing all cypress tests. \n'); | ||
|
||
exec( | ||
"docker ps -a | awk '{ print $1,$2 }' | grep cypress/included | awk '{print $1 }' | xargs -I {} docker kill {}" | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcalcaben can you please verify the docs?