When you work with end to end tests in Cypress you may notice they quickly get time-consuming for running the whole test suite. The more complex the project the more test cases you may end up with and this can take a long time to run - dozens of minutes or even hours.
One way of executing faster tests in Cypress is to use CI parallelism which is essentially running a few chunks of test suite spread across parallel jobs on CI server. In this article, you will learn how to configure Codefresh.io server with matrix feature to run parallel steps for your Cypress tests and this way save time on faster CI builds.
Configure Codefresh.io to run Cypress.io tests
You need in your project repository a Dockerfile that will be used by Codefresh to build Docker image which will be used to run Docker container with your Cypress tests inside of it.
Next thing is to add
.codefresh/codefresh.yml file with instructions to run your application HTTP server in the background and then run Cypress tests against the application.
In the below example, you will see that you can leverage matrix environment configuration for Codefresh. Set a few environmental variables with the node index for each parallel step. This is required by Knapsack Pro tool. Knapsack Pro is a wrapper around Cypress test runner which is responsible for running a proper set of tests across parallel jobs (parallel steps on Codefresh).
Knapsack Pro for Cypress has built-in Queue Mode mechanism which splits test files in a dynamic way across parallel CI nodes (parallel jobs) and thanks to that ensure each parallel step takes a similar time so your CI build is executed in optimal time. You save time by avoiding a not optimal split of tests. You simply eliminate bottleneck slow job that could happen when you allocate too many slow test files to the same parallel step.
You will learn more how exactly Queue Mode works at the end of this article but for now, let’s take a look at Codefresh YAML configuration.
You also need to add an API token for the Cypress test suite. You can get the API token at Knapsack Pro.
- You need to set an API token
KNAPSACK_PRO_TEST_SUITE_TOKEN_CYPRESSin Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to the pipeline) -> Variables tab (see a vertical menu on the right side).
- Set where Codefresh YAML file can be found. In Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Workflow tab (horizontal menu on the top) -> Path to YAML (set there
- Set how many parallel jobs (parallel CI nodes) you want to run with
KNAPSACK_PRO_CI_NODE_TOTALenvironment variable in
- Ensure in the
matrixsection you listed all
KNAPSACK_PRO_CI_NODE_INDEXenvironment variables with a value from
KNAPSACK_PRO_CI_NODE_TOTAL-1. Codefresh will generate a matrix of parallel jobs where each job has a different value for
KNAPSACK_PRO_CI_NODE_INDEX. Thanks to that Knapsack Pro knows what tests should be run on each parallel job.
How dynamic test suite split works in Queue Mode
You can learn how dynamic test suite split with Knapsack Pro Queue Mode works in the below video. There are examples of a few edge cases that Queue Mode helps with.
If you are considering using Cypress in your project, check out our comparison of Cypress.io with other solutions. And to compare Codefresh CI to other CIs, you can visit our comparison pages: Codefresh vs CircleCI, Codefresh vs Jenkins, Github Actions vs Codefresh CI, and Codefresh vs Other CI providers.