Dany Paredes
Dany Paredes | Javascript / Web

Dany Paredes | Javascript / Web

Code coverage in 2 minutes with NYC

Code coverage in 2 minutes with NYC

Dany Paredes's photo
Dany Paredes
·Aug 12, 2020·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

I didn’t have a clear picture of how much code has tests? NYC comes to help me know the actual 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. In my example, we install NYC to read the results from Mocha, and it shows the % of cover.

I’m using the same project for testing if you want 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 ready, then run the command to see the report.

NYC npm run test

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

We can see the branches, lines, functions with and without tests, files, and percentages covered. If you check the test ads.spec.js, it only contains the first switch case, and the report notifies the lines 11,18,25 are not covered. If I want to get 100% of the 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

Rerun the NYC from the terminal, and the report shows 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 it, please share it. Thanks for reading!

Photo by Clay Banks on Unsplash

Did you find this article valuable?

Support Dany Paredes by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this