Dany Paredes
Aug 28 · 3 min read

In my previous posts about testing, I don’t have a clear picture of how much of my code is protected with tests? NYC comes to help me know the real status of my testing.

NYC is an npm package for getting stats about the test coverage working hand to hand with Mocha and the setup is so easy. My example is setup NYC to read the results from Mocha and it shows the % of cover.

Image by Gerd Altmann from Pixabay

I’m using the same project for testing if you want you to check the changes in the repo Github or continue reading.

Install NYC

Install the package NYC using npm, it will help to get the report of our test coverage with Mocha.

npm install NYC --save-dev

Running coverage

NYC is already installed, then run the command to see the report.

NYC npm run test

NYC it will generate a report table with stats about the code, functions and test cover by our tests.

We can see the branches, lines, functions with and without test, files and the percentage of covered, if you check the test ads.spec.js it only contains the first switch case, and the report notify the lines 11,18,25 are not covered, If I want to get 100% of cover, I will complete my pending cases.

Completing the cases

Edit the ads.spec.js file and include the pending cases and the default.

const assert = require('assert');
const { getIVABanner } = require('../src/ads');
describe('Ads for taxes', () => {
it('Get green banner for 15% IVA', () => {
const actual = getIVABanner(15);
const expect = {
img: '/bar.jpg',
size: '15%',
border: 3,
color: 'green'
}
assert.deepEqual(actual, expect)
})
it('Get green banner for 20% IVA', () => {
const actual = getIVABanner(20);
const expect = {
img: '/bar.jpg',
size: '20%',
border: 5,
color: 'yellow'
}
assert.deepEqual(actual, expect)
})
it('Get green banner for 30% IVA', () => {
const actual = getIVABanner(30);
const expect = {
img: '/bar.jpg',
size: '30%',
border: 5,
color: 'red'
}
assert.deepEqual(actual, expect)
})
it('Get default ', () => {
const actual = getIVABanner();
const expect = {
img: '/bar.jpg',
size: '100%',
border: 5,
color: 'red'
}
assert.deepEqual(actual, expect)
})
})

The Run the test

Run again the NYC from the terminal to show our test and it shows the report with 100% of our code covered.

nyc npm run test

That’s it!

Hopefully, that will give you a bit of a head-start in code covered with Mocha and NYC. If you enjoyed please share it. Thanks for reading!

danywalls

My Digital life journal

Dany Paredes

Written by

A few years ago I was just a .NET guy, but in my last years I have been focusing on Front-end, building solutions with Typescript, Angular, Vue, React and Sass.

danywalls

danywalls

My Digital life journal

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade