DEV Community

Cover image for Code coverage in 2 minutes with NYC
Dany Paredes
Dany Paredes

Posted on • Updated on

Code coverage in 2 minutes with NYC

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.

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

Running coverage

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

NYC npm run test
Enter fullscreen mode Exit fullscreen mode

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

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

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!

Photo by Clay Banks on Unsplash

Top comments (1)

Collapse
 
inane profile image
inane

In my scenario is passing my tests but statistics are 0%. Any clue about this?