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
The uncovered lines marked in yellow are part of code branches (if-else statements, ternary operators or else). Corrsponding to that you can see that our branch coverage is at 75% and therefore not complete yet.
Jest, a flexible, easy-to-use testing framework, will be used to test a simple Typescript application. Codecov, a tool for monitoring code coverage, will be used to measure the test coverage for your Jest unit tests.
Code coverage makes possible to spot untested paths in our code. It is an important metric for determining the health of a project. I've seen enough misconfigured JavaScript projects where Jest coverage were reported incorrectly.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With