Reusable Cypress workflows for GitHub Actions
Call these workflows from your GitHub Action workflows, a single line (with parameters) let's you run N parallel test jobs without any configuration.
π Covered in my course Testing The Swag Store and Cypress-split plugin
Check out the source code, install and cache dependencies, and run all Cypress specs using the following workflow, store the cypress/screenshots, cypress/videos, and cypress/results artifacts.
In your .github/workflows/ci.yml use the following:
name: ci on: [push] jobs: test: # use the reusable workflow to check out the code, install dependencies # and run the Cypress tests # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1default true. Stores the test run artifacts.
A common scenario is to start the application and wait for it to respond. You can pass parameters to the workflow with the start command and the address to wait for.
name: ci on: [push] jobs: test: # use the reusable workflow to check out the code, install dependencies # and run the Cypress tests # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1 with: start: npm start wait-on: 'http://127.0.0.1:3000'To record the test run on the Cypress Dashboard, pass the options to the workflow and the record key as a secret
name: ci on: [push] jobs: test: # use the reusable workflow to check out the code, install dependencies # and run the Cypress tests # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1 with: record: true secrets: recordKey: ${{ secrets.CYPRESS_RECORD_KEY }}Let's split all specs across 3 machines using cypress-split plugin. Configure the plugin using its documentation, then use this workflow:
name: ci on: [push] jobs: test: # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: nE2E: 3Sometimes you might want to run a single command before all split jobs start. You can use before-run parameter. See rn-examples
name: ci on: push jobs: component-tests: # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: # print the test names before-run: 'npm run test-names --silent' nComponent: 2If you click on the "prepare" job, the before-run step prints the test names
Combines all separate Mochawesome JSON reports into a single HTML report including screenshots and videos.
# https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: nE2E: 3 marge: trueAssumes the project has installed mochawesome, mochawesome-merge, and mochawesome-report-generator dependencies.
import { defineConfig } from 'cypress' export default defineConfig({ // https://github.com/adamgruber/mochawesome reporter: 'mochawesome', reporterOptions: { useInlineDiffs: true, embeddedScreenshots: true, reportDir: 'cypress/results', reportFilename: '[name].html', overwrite: true, html: true, json: true, } })See the example in bahmutov/cy-report-example and read the blog post The Battle of Cypress Mochawesome Reporters.
Warning: make sure the reporter options are shown as above, including json: true. For example, you might accidentally overwrite the reporter in the e2e block:
export default defineConfig({ // https://github.com/adamgruber/mochawesome reporter: 'mochawesome', // all good reporterOptions: { useInlineDiffs: true, embeddedScreenshots: true, reportDir: 'cypress/results', reportFilename: '[name].html', overwrite: true, html: true, json: true, }, e2e: { baseUrl: 'http://localhost:3000', // π¨ Oops, wrong reporter for merging reporter: 'cypress-junit-reporter', } })If you use @bahmutov/cypress-code-coverage you can combine the coverage from each split run into a single report
# https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: nE2E: 3 coverage: trueLet's split all tests across 3 machines using Cypress Parallelization paid feature.
name: ci on: [push] jobs: test: # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/parallel.yml@v1 with: n: 3 secrets: recordKey: ${{ secrets.CYPRESS_RECORD_KEY }}Result:
- standard.yml checks out code, installs dependencies, and runs tests on a single machine
- split.yml checks out the coe, installs dependencies, splits specs per machine using cypress-split plugin.
- parallel.yml lets you specify the number if test machines to use. Splits specs using Cypress Dashboard
The workflows allow passing pretty much all Cypress GH Action parameters, see the individual workflow YML file.
If you are using cypress-split and the split workflow, you can generate timings for each spec and combine them into a single file. That file can then be used to split the specs more efficiently than alphabetically. You can even commit the file back to the repo using the same GitHub Token. See the example in bahmutov/cypress-split-timings-example.
name: split on: # launch this workflow from GitHub Actions page workflow_dispatch: jobs: split: # https://github.com/bahmutov/cypress-workflows uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: nE2E: 2 # use timings to split E2E specs across 2 machines efficiently split-file: 'timings.json' # this job grab the output for the `split` workflow # and writes it into a JSON file "timings.json" # and then commits the updated file to the repository commit-updated-timings: if: github.ref == 'refs/heads/main' runs-on: ubuntu-22.04 needs: split steps: - name: Checkout π uses: actions/checkout@v4 - name: Show merged timings π¨οΈ run: | echo off echo '${{ needs.split.outputs.merged-timings }}' - name: Write updated timings πΎ # pretty-print json string into a file run: echo '${{ toJson(fromJson(needs.split.outputs.merged-timings)) }}' > timings.json - name: Commit changed spec timings β±οΈ # https://github.com/stefanzweifel/git-auto-commit-action uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Updated spec timings branch: main file_pattern: timings.jsonAdvice: use an explicit release tag when using a workflow like parallel.yml@v1.0.1. You might also use the latest release from the major branch v1 like parallel.yml@v1. Not recommended: using the latest commit on the branch parallel.yml@main or using a specific commit parallel.yml@2a9d460.
Instead of separate E2E and component test jobs, use a single job and split it as you would like
# v1 jobs: e2e: uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v1 with: n: 3 component: uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v1 with: component: true n: 2 # v2 jobs: tests: uses: bahmutov/cypress-workflows/.github/workflows/split.yml@v2 with: nE2E: 3 # run 3 parallel jobs for E2E tests nComponent: 2 # run 2 parallel jobs for component tests- see docs/examples.md
- bahmutov/cypress-workflows-example shows how to use the standard and the parallel workflows
- cypress-3rd-party-script-example shows how to run the end-to-end tests using the standard workflow before deploying the site
- cypress-wordle
- react-app-actions shows how to run the tests using a workflow and then release a new version using semantic release step
- bahmutov/cypress-recurse
- bahmutov/my-svelte-app
- bahmutov/sudoku-testing-v10 runs E2E and component tests using Cypress v10
- Run Cypress Specs In Parallel For Free
- Trigger Selected Cypress Specs Using GitHub Actions
- Run Cypress Specs In Parallel For Free Using Spec Timings
- Quickly Run The Changed Specs on GitHub Actions
You can print the workflow inputs using the parameter debug-inputs, for example
uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1 with: debug-inputs: trueShould print something like
Debug inputs π { "record": false, "config": false, "config-file": "", "envs": "grepTags=@nightly", ... "quiet": false, "debug-inputs": true } You can set the environment variable DEBUG to enable verbose output from the debug module, often used by Cypress and its plugins.
uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1 with: debug: cy-grepShould print something like:
cy-grep cy-grep plugin version 1.3.1 +0ms cy-grep Cypress config env object: { grepFilterSpecs: true, grepOmitFiltered: true, CACHE_FOLDER: '/home/runner/.cache/Cypress' } +1ms cy-grep specPattern cypress/e2e/**/*.cy.{js,jsx,ts,tsx} +2ms cy-grep excludeSpecPattern *.hot-update.js +0ms Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> Β© 2021
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2021 Gleb Bahmutov <gleb.bahmutov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




