Skip to content

Instantly share code, notes, and snippets.

@DanteDeRuwe
Last active January 30, 2024 08:06
Show Gist options
  • Save DanteDeRuwe/3c7c4e2073b215241716aef1a7ce8201 to your computer and use it in GitHub Desktop.
Save DanteDeRuwe/3c7c4e2073b215241716aef1a7ce8201 to your computer and use it in GitHub Desktop.
Run cypress tests in parallel locally
########################################################################
# RUNS ALL .cy.ts FILES IN THE cypress/e2e FOLDER IN PARALLEL
# ARGUMENT $1: The path to the directory containing the cypress folder
########################################################################
baseCommand="npm run dev --" # change this if needed
echo ""
# validate argument
if [ -z $1 ]; then
echo "No path provided. Please provide the path to the directory containing the cypress folder as the first argument."
exit 1
fi
cypressDir="$1/cypress"
# Clean up the cypress folders before running the tests
echo "Cleaning up cypress screenshots, reports and downloads..."
rm -rf $cypressDir/screenshots && rm -rf $cypressDir/reports && rm -rf $cypressDir/downloads && echo "Done cleaning!"
echo ""
echo "************************************************************************************"
echo "***************** STARTING PARALLEL TEST RUN *****************"
echo "**** Note: output might appear slow but multiple tests ARE running in parallel. ****"
echo "************************************************************************************"
echo ""
start_time=$(date +%s)
files=()
commands=()
# Iterate over the files in the cypress e2e directory and create commands for each file
for file in $cypressDir/e2e/**/*.cy.ts; do
cmd="$baseCommand -q --spec '$file'"
commands+=("$cmd")
files+=("$(basename $file)")
done
# Create comma-separated list of file names. This is used to name the processes.
filesCsv=$(IFS=,; echo "${files[*]}")
# Run the commands in parallel using concurrently with the following options:
#
# --max-processes="50%" Use max 50% of CPU cores to batch the processes
# --handle-input Pass input to child processes (mainly for ctrl+c)
# --group Order the output as if the commands were run sequentially.
# --prefix-colors "auto" Automatically color output
# --names $filesCsv Name the processes after the files
# "${commands[@]}" Pass all commands as arguments to concurrently
#
npx concurrently \
--max-processes="50%" \
--handle-input \
--group \
--prefix-colors "auto" \
--names $filesCsv \
"${commands[@]}"
end_time=$(date +%s)
# Calculate the time
difference=$(( $end_time - $start_time ))
echo -e "\n\n\n\n\n"
echo "************************************************************************************"
echo "********************* DONE! **********************"
echo "********************* Full test run took $difference seconds. **********************"
echo "************************************************************************************"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment