Run Cypress tests automatically using GitHub Actions
Preconditions (2)
✅ Expected Result: GitHub Actions workflow runs Cypress tests automatically on each push or pull request and reports success or failure in the Actions tab
name: Cypress Tests
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Run Cypress tests
run: npx cypress run
This GitHub Actions workflow is named Cypress Tests. It triggers on push and pull_request events targeting the main branch.
The job cypress-run runs on the latest Ubuntu runner.
Steps include:
actions/checkout@v3 to get the code.actions/setup-node@v3 to install Node.js version 18.npm install to install project packages.npx cypress run to execute tests.This setup ensures tests run automatically on GitHub servers whenever code changes are pushed or pull requests are created, providing quick feedback on test results.
Now add caching for node_modules to speed up workflow runs
on key defines events like push or pull_request that start the workflow.run is used inside steps, steps lists commands, jobs groups tasks.on:
push:
branches:
- main
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Cypress tests
run: npx cypress run
main branch only.npx cypress run.on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
run: cypress run
What is the most likely error?npx cypress run to run Cypress installed locally.Pass the test and this pattern moves unknown