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
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@v1
default 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: 3
Sometimes 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: 2
If 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: true
Assumes 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.
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: true
Let'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.
Advice: use an explicit release tag when using a workflow like [email protected]
. 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
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: true
Should 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-grep
Should 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 <[email protected]> © 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 <[email protected]>
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.