skies.dev

Run Cypress Tests against Vercel Preview Deployments

3 min read

GitHub Actions is a clean way to run Cypress against every successful Vercel deployment. That gives you a real browser check on the exact preview URL your team is about to share.

To follow along, you should already know the basics of:

  • GitHub Actions
  • Deploying with Vercel
  • Cypress

For a deeper walkthrough, see Gleb Bahmutov's article on testing preview Vercel deploys.

Trigger the workflow from deployment events

Start by creating .github/workflows/cypress.yml.

.github/workflows/cypress.yml
name: 'Run Cypress tests'

Listen for deployment_status events so the workflow runs only when Vercel has finished a deploy.

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [ deployment_status ]

Next, define an e2e job.

Gate the job on a successful deployment so failed or still-pending deploys do not waste CI minutes.

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [ deployment_status ]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'

Add the Cypress Action

Once the deployment succeeds, the workflow should:

  1. set up an Ubuntu environment
  2. checkout our branch
  3. run the Cypress tests

Use Cypress's first-party GitHub Action to run the browser tests.

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [ deployment_status ]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run Cypress
        uses: cypress-io/github-action@v2

Configure environment variables

Set Cypress environment variables for the deployment URL, secrets, and retry behavior.

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [ deployment_status ]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run Cypress
        uses: cypress-io/github-action@v2
        env:
          CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
          CYPRESS_SECRET: ${{ secrets.CYPRESS_SECRET }}
          CYPRESS_RETRIES: 1

Upload artifacts on failure

If the run fails, upload the Cypress videos so you can inspect the failure later. GitHub stores them as artifacts.

Keep retention-days short so the artifacts do not pile up.

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [deployment_status]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run Cypress
        uses: cypress-io/github-action@v2
        env:
          CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
          CYPRESS_SECRET: ${{ secrets.CYPRESS_SECRET }}
          CYPRESS_RETRIES: 1
      - uses: actions/upload-artifact@v2
        if: failure()
        with:
          retention-days: 1
          path: |
            cypress/videos

With this workflow in place, every successful Vercel deployment gets a Cypress check automatically.

Final workflow

.github/workflows/cypress.yml
name: 'Run Cypress tests'
on: [deployment_status]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run Cypress
        uses: cypress-io/github-action@v2
        env:
          CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
          CYPRESS_SECRET: ${{ secrets.CYPRESS_SECRET }}
          CYPRESS_RETRIES: 1
      - uses: actions/upload-artifact@v2
        if: failure()
        with:
          retention-days: 1
          path: |
            cypress/videos

Hey, you! 🫵

Did you know I created a YouTube channel? I'll be putting out a lot of new content on web development and software engineering so make sure to subscribe.

(clap if you liked the article)

You might also like