hyperswitch icon indicating copy to clipboard operation
hyperswitch copied to clipboard

[CYPRESS_FRAMEWORK] Run Cypress tests in parallel

Open pixincreate opened this issue 1 year ago • 2 comments

Description:

Cypress tests are resource intensive and consume a lot of memory and power. But that should not stop us from running them in parallel even if they consume a lot of resources. At present, Cypress tests here at Hyperswitch is run in sequential and consumes a lot of time given that each connector consumes at least 10+ mins and we've 10+ connector to run tests for.

With that said, the task is to parallelize the tests that are run and with that said, the consumption can be reduced drastically (1+ hour(s) to mere 15 mins).

[!IMPORTANT] Cypress cloud is a no go

Below is the shell script that runs helps run the test in both sequential and parallel ways:

#!/bin/bash

CONNECTORS=("adyen" "bankofamerica" "bluesnap" "cybersource" "iatapay" "nmi" "paypal" "stripe" "trustpay")

# Function to run tests
run_tests() {
  mkdir -p results
  local pids=()

  for connector in "${CONNECTORS[@]}"; do
    if [[ $parallel == true ]]; then
      CYPRESS_CONNECTOR="${connector}" npm run cypress:ci > results/${connector}.txt 2>&1 &
      pids+=($!)
    else
      printf "\rRunning Cypress tests against the connector: ${connector}"
      CYPRESS_CONNECTOR="${connector}" npm run cypress:ci > results/${connector}.txt 2>&1
      local exit_code=$?
      if [[ $exit_code -ne 0 && $exit_code -ge 128 ]]; then
        exit 1
      fi

      sleep 3
    fi
  done

  if [[ $parallel == true ]]; then
    for pid in "${pids[@]}"; do
      wait $pid
    done
  fi
}

# Check if running in parallel
if [[ "${1}" == "--parallel" ]]; then
  printf "WARNING: Parallel tests are flaky and may not work as expected.\n"
  eval run_tests true
else
  eval run_tests false
fi

To execute the script:

# to run tests sequentially, which can take an hour+
sh run-cypress.sh
# to run tests in parallel, consumes around 15 - 20 mins, at the cost of resources
sh run-cypress.sh --parallel

Getting started:

Go through the README for guidelines on file structure, formatting and instructions on how to set up and run Cypress.

In short:

cd cypress-tests
npm ci

Possible implementation:

  • Shell script above is attached for reference
  • Make use of gnu-parallel to reduce the complexity of the script. The advantage here is that, it also make the script more future proof, manageable, easier to read and understand
  • As a bonus, the same functionality can be implemented in JavaScript directly into Cypress via its configs is a plus

[!WARNING] Make sure that before this task is picked, the machine is capable enough to run at least 2 connector tests in parallel. Running Cypress tests in parallel is too resource intensive! Please go through the Cypress docs to learn whether you're machine meets minimum requirements.

pixincreate avatar Sep 16 '24 17:09 pixincreate

This is a P0, I'll be picking this up.

CC: @gorakhnathy7

pixincreate avatar Sep 27 '24 07:09 pixincreate

this was parked until recently due to other priority tasks, will pick this again, soon.

pixincreate avatar Oct 03 '24 06:10 pixincreate

Sure @pixincreate

gorakhnathy7 avatar Oct 04 '24 02:10 gorakhnathy7