DEV Community

Rukundo Kevin
Rukundo Kevin

Posted on

GitHub Action: With Coveralls

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.

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 nycthe Istanbul cli.

"scripts": {
    "test": "mocha --recursive --require @babel/register",
    "test-coverage": "nyc npm test && nyc report --reporter=text-lcov"}
Enter fullscreen mode Exit fullscreen mode

--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 }}
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

We used Coveralls' GitHub Action which can be found here.

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
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

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

Follow me on Github

Top comments (0)