Aman Kumar
Aman Kumar

Reputation: 399

How to get the uncovered parts of code in jest

I wrote a simple reducer :

const simpleCounterReducer = (state = 0, action) => {
    switch (action.type) {
        case 'INCREMENT_COUNT':
            return state + 1;
        case 'DECREMENT_COUNT':
            return state - 1;
        default:
            return state;
    }
};

export default simpleCounterReducer;

And then a few simple tests to cover all the possible options.

import simpleCounterReducer from '../src/reducers/simple-counter.js';

describe('counter works and', () => {
    test('can handle increments', () => {
        expect(
            simpleCounterReducer(0, {
                type: 'INCREMENT_COUNT'
            })
        ).toBe(1);
    });

    test('can handle decrements', () => {
        expect(
            simpleCounterReducer(1, {
                type: 'DECREMENT_COUNT'
            })
        ).toBe(0);
    });

    test('can handle invalid actions', () => {
        expect(
            simpleCounterReducer(4, {
                type: 'SOME_RANDOM_ACTION'
            })
        ).toBe(4);
    });
});

Then I ran this command : npx jest --colors --coverage

But even though I have covered all parts of the code, I am getting an uncovered line. Is it something wrong with jest or am I missing something. And is there a way to find out in jest, the parts of code that are not covered.

Image for jest test coverage output

Upvotes: 21

Views: 32093

Answers (1)

BenoitVasseur
BenoitVasseur

Reputation: 782

If you want to see the lines that are not covered you can open in a browser the generated report.

By default the report is here ./coverage/lcov-report/index.html.

But you also see in the console the line numbers of the uncovered lines (it is not the number of lines that are not cover but the line numbers, and in your case it is the first line).

Also some config for the coverage if needed : https://jestjs.io/docs/en/configuration#collectcoverage-boolean

Side note, it uses istanbul behind the scene : https://github.com/gotwarlost/istanbul

Upvotes: 37

Related Questions