Enable Tests in the Continuous Integration Pipeline

Learn how to make sure that tests run as part of the continuous integration pipeline so you don't have to run them manually.

Running test suites and recording the results in the Cypress dashboard is, at the time of this writing, as good as it gets when it comes to testing. However, we want to make sure tests run as part of the continuous integration pipeline so developers don’t have to run them manually. We also want the results displayed right in the pull request view and to block a PR from being merged if tests fail.

Set up GitHub workflow

Let’s implement that! We already have a .github/workflows/services-web-deploy.yml file that deploys the web service. It is a good time to rename this file now since it is no longer only deploying, but will soon also be testing the web service. In fact, having one workflow file per service is a good idea going forward. Let’s rename that workflow file to services-web.yml. Click Run and execute the following command in the terminal:

mv .github/workflows/services-web-deploy.yml .github/workflows/services-web.yml

This makes it clear to anyone who sees the file name that it is responsible for anything related to the services/web code.

First, change the name of the workflow to “CI for the web service”:

name: CI for the web service

We need to make the next change to configure the workflow to run on every push when files in services/web/** change. Currently, it only runs on the master branch because we only want to deploy on the master branch. Let’s remove that condition to make it look like this:

on:
  push:
    paths:
      - "services/web/**"

Next, add a new job above the already existing deploy job:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: npm install
      - name: Bootstrap all services
        run: npm run bootstrap
      - name: Run e2e tests
        uses: cypress-io/github-action@v2
        with:
          install: false
          record: true
          start: npm run dev
          wait-on: " http://localhost:3000 "
          working-directory: services/web
        env:
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_ENV: test
  deploy:
    ...

Click Run to save the changes in this file.

Get hands-on with 1200+ tech skills courses.