GitHub Action: With Coveralls
Tags: githubactions, testing, devops, github
Published: 2023-03-07 11:29:27

In the previous articles, we created a GitHub Action workflow for one or multiple NodeJS project.

Today, we are going to go through the steps required to generate code coverage badge using [coveralls](https://coveralls.io/).

**Step 1: Connect Coveralls with GitHub** Go to Coveralls and Under "Add repo", add the repository you want to include.

**Notice that with the free tier, the repository has to be public.**

**Step 2 : Add the test coverage script** 
If you use jest to run your tests, you can run jest --coverage to create a coverage report but if you are using another testing framework like Mocha, you can use [nyc](https://istanbul.js.org/)the Istanbul cli.

```
"scripts": {
    "test": "mocha --recursive --require @babel/register",
    "test-coverage": "nyc npm test && nyc report --reporter=text-lcov"}
```
_--recursive_ option looks for tests in subdirectories recursively
_--reporter=lcov_ means it will create a ./coverage/lcov.info to save the coverage results. We only need to the results on the CI pipeline, if you run this command locally, you can safely gitignore or delete the results again.
 
**Step 3: Add test coverage to our CI workflow**
So we will replace the _npm run test_ in our previous workflow with _npm run test-coverage_ and another step to the workflow

```
# Send coverage report to Coveralls
    - name: Coveralls
      uses: coverallsapp/github-action@master
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
```
Now the new workflow will look like this,

```
# This workflow will do a clean install of node dependencies, build the source code and run tests
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: CI Pipeline

# trigger build when pushing, or when creating a pull request
on: [push, pull_request]

jobs:
  build:

    # run build on latest ubuntu
    runs-on: ubuntu-latest

    steps:
    # this will check out the current branch (https://github.com/actions/checkout#Push-a-commit-using-the-built-in-token)
    - uses: actions/checkout@v3
    # installing Node
    - name: Use Node.js 16.16.0
      uses: actions/setup-node@v3
      with:
        # this will use the latest Node 16 version
        node-version: 16.16.0
    # install dependencies using clean install to avoid package lock updates
    - run: npm ci
    # build the project if necessary
    - run: npm run build --if-present
    # finally run the tests
    - run: npm run coverage

    # Save coverage report in Coveralls
    - name: Coveralls
      uses: coverallsapp/github-action@master
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
```
We used Coveralls' GitHub Action which can be found [here](https://github.com/marketplace/actions/coveralls-github-action).

Note that GITHUB_TOKEN is automatically passed and does not have to be configured by you in any way. Since our GitHub account is linked with Coveralls, there is also no additional setup necessary to authenticate your Coveralls account.

Now commit & push your changes and wait for the build to finish.

**Step 4: Adding the badge**

Sign in to coveralls and navigate to your repository. and click on upper embed in badge tab. 

![Coveralls test badge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntszyxmgeud1sum9aso0.jpg)
 Copy the Markdown, go and paste it in your repo's README.

After all that you should be able to see your coverage badge like this one( Hope your coverage a higher than 70 )

![Coverage results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ng6lm8r5hygx1tg5hip.jpg)

That's all there is to it. Now all that's left is to set up the CD workflow. 

Follow me on [Github](https://github.com/rukundo-kevin)