Without a doubt, one of the aspects a developer should pay more attention is trying to always generate understandable, maintainable and clear code, in short, to generate clean code.
During the development of code (modules, libraries) it is important to integrate objective tools that measure the status of the code and provide the information to know its quality and thus be able to detect and prevent problems: duplicate functions, excessively complex methods, code low quality, non-standard coding style.
This code coverage report is automated in the continuous integration system (GitHub Actions) and are executed in each new version of the software.
Here you have the technologies used in this project
- ASK CLI - Install and configure ASK CLI
- GitHub Account - Sign up here
- Codecov Account - Register here
- Node.js v10.x
- Visual Studio Code
Codecov provides highly integrated tools to group, merge, archive and compare coverage reports. Whether your team is comparing changes in a pull request or reviewing a single commit, Codecov will improve the code review workflow and quality. Codecov supports the most commons CICD engine like the one we use, GitHub Actions.
As the official Codecov documentation says, here are the things you'll need or want to have in place before using Codecov:
- Sign up on codecov.io and link either your GitHub, GitLab, or Bitbucket account.
- Once linked, Codecov will automatically sync all the repositories to which you have access. You can click on an organization from your dashboard to access its repositories, or navigate directly to a specific repository using: https://codecov.io/\/<account-name>/<repo-name>. Example: https://codecov.io/gh/xavidop/alexa-nodej-lambda-helloworld.
- Get the token of this repo generated by Codecov that we will use in our pipeline.
Now we can go back to our code to set up the project
After this, we can install codecov
Node.js uploader library using npm. --save-dev
is used to save the package for development purpose. Example: unit tests, minification
npm install codecov --save-dev
It is important to notice that the code coverage report will be generated by an external tool called nyc
that its output will be used by this uploader library in order to upload it to Codecov. Let's go deeper!
Once we have codecov
Node.js uploader library now we have to configure it.
In order to use the uploader library, we need to set an environment variable called CODECOV_TOKEN
with the value that Codecov has generated in the previous step.
After that we have to create the codecov.yml
configuration file in the root of our repository with this content:
fixes:
- "src/::lambda/src/"
Why? well, our JavaScript code is not located in the root of our project so when we run the code coverage report it will start from src/
subfolder.
So we have to change the folder of the report to set the correct path starting from the root directory of the repo.
With this yaml file Codecov will change all file paths from src/
to lambda/src
.
This change is needed if you want to navigate between files in the Codecov UI online.
This is not a problem only with Codecov, I have the same issue with Coveralls tool for example.
Once we have everything configured, we have to set up the report we are going to use to check the Code Coverage.
We will use the npm package nyc
which is the CLI tool of the famous npm package Istanbul
.
Both nyc
and Istanbul
are the most used code coverage libraries for Node.js and JavaScript projects
You can install nyc
using npm. --save-dev
is used to save the package for development purpose. Example: linters, minification.
npm install nyc --save-dev
This npm package has an option to set the output format of the report to a .lcov
type which is the type that codecov
Node.js uploader library needs to upload to Codecov website.
After the report has been updated to Codecov, this is how this report looks like:
Now it is time to integrate it into our package.json
to use it in our pipeline with npm run
command!
So, in this file we are going to add the following commands in the script
json node:
codecov
: this command will execute the Code Coverage report and uploads it to Codecov:nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov
Everything is fully installed, configured and integrated, let's add it to our pipeline!
This job will execute the following tasks:
- Checkout the code
- Run
npm run codecov
to execute the Code Coverage and then upload it to Codecov.
codecov:
runs-on: ubuntu-latest
name: Code Coverage
needs: test
steps:
# To use this repository's private action,
# you must check out the repository
- name: Checkout
uses: actions/checkout@v2
- run: |
cd lambda;
npm install;
npm run codecov
env: # Or as an environment variable
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
NOTE: Remember to set the CODECOV_TOKEN
secret in GitHub Actions.
- DevOps Wikipedia - Wikipedia reference
- Official Codecov Documentation - Codecov Documentation
- Official GitHub Actions Documentation - Official GitHub Actions Documentation
A minimum of quality and focus our efforts on testing is the most complicated and important parts of our business. Tools like Codecov help us to achieve these goals and are a fundamental in continuous improvement environments.
I hope this example project is useful to you.
That's all folks!
Happy coding!