Code coverage implemented: RSpec and Jest unite in Codecov!

In my previous post, I have discussed about some theory of code coverage and stuffs. In this post, I will talk about how to produce code coverage percentage with RSpec and Jest, and how to merge the result by utilizing Codecov script.

I have been struggling with implementing a procedure to compute code coverage in our project. Installing and producing coverage result is actually fairly easy. There are so many libraries out there in the internet, but usually the solutions offered in the internet assume that only one language is used in the repository. Of course this solution will not work for repos with multiple language, such as ones with microservices located in one repo, or ones with backend and frontend service written different language.

Our project is one of those, we use Ruby-on-Rails for managing the route, API and behaviour of the backend services, and we use ReactJS to implement the behaviour of the frontend services / components.

After reading tons of articles and documentations, I came up with some solutions, one of which to merge the results produces by multiple test suites manually using script. I almost do it, before I try reading the superb documentation of codecov once again. Another beautiful solution is, to just use `bash script uploader` provided by CodeCov!

Let’s go on details, steps:
1. Install and load simplecov for ruby (rails)
2. Enable code coverage collectors in Jest
3. Run the test, and upload them to codecov using their made-in-heaven scripts!

Note that in this post, I assume that you already have a repo with rails, rspec and jest already installed. If you don’t know how to, ask Salsa.

Step 1 : Load Simplecov

Simplecov is a simple library written to calculate code coverage for codes written with ruby. They also come built-in to specially support Rails (Yes! who wrote ruby app without rails?).

Install simplecov by adding simplecov to Gemfile and run `bundle install`

# Gemfile

gem 'simplecov-json'

We use simplecov-json because we only want to output the coverage report in JSON format (so codecov script can track it later).

Next, require and load simplecov in spec/spec_helper.rb

# spec/spec_helper.rb

require 'simplecov'
require 'simplecov-json'

SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start 'rails'

done! try running the test by using `bundle exec rspec`, a new directory `coverage` will be created with a file `coverage.json` inside it.

Step 2 : Enable Coverage in Jest

Enabling code coverage report in Jest is fairly simple and easy (thanks to their great team).
Add these two lines in package.json:

# package.json

"jest": {
 ..., // previous configs

"collectCoverage": true,
 "coverageReporters": ["json"]

done! try running the test by executing command `jest`, a new file inside folder `coverage` will appear, named `coverage-final.json`. If you got some errors like jest not found, try running `node ./node_modules/jest-cli/bin/jest.js`


Tips! It might not be nice to remember all those commands to execute our tests. So my team came up with a solution by adding those scripts in npm script.

# package.json

"scripts": {
 "test": "echo 'Running RSpec...' && bundle exec rspec && echo 'Running jest...' && node ./node_modules/jest-cli/bin/jest.js"

Step 3 : Merge and Upload them to Codecov

To upload a coverage report to, you will need to have the repository token for your repo. You can access them by visiting

After you got the token add them to a file .codecov.yml in the repository root

# .codecov.yml

 token: [your-repo-token]

And the last thing to do is upload the coverage results. In their documentation, they state that they are smart enough that you can upload multiple coverage results not pararelly, and still detect them. But there is a more beautiful solution (it is my personal opinion though), it is to just use their bash uploader script.

curl -s | bash -s -

If you run that command, the script will exhaustively try to track common possible result files in different format (in my case, I only output JSON format, just because). Then, they will merge them in tmp file and upload them to your repo page in

Executing the command manually is of course a no-go, so I created another npm script to run them after running tests

# package.json

"scripts": {
 "test": "echo 'Running RSpec...' && bundle exec rspec && echo 'Running jest...' && node ./node_modules/jest-cli/bin/jest.js",
 "test-coverage": "npm run test && curl -s | bash -s -"

Done! If you run `npm run test-coverage`, tests will run and after that, the coverage results will automatically merge d and uploaded to, you can view the commit diffs and coverage details in


If you read this post, it seems to be very easy, but just for your information, I spend my days in experimenting the most elegant way to do these stuffs. Trial-and-Error is needed, I even created my own copy of our project repo for the experiment. I believe that there are other elegant solutions, but this is a solution I came up with, so give me my beer floridina!






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s