I'm having trouble achieving 100% Jest Line Coverage. Jest is showing uncovered lines on lines a have a return statement inside of a branching statement.
export class Galactic {
constructor(earthAge) {
this.earthAge = earthAge;
this.mercuryAge = 0; //.24 earth years
this.venusAge = 0; //.62
this.marsAge = 0; //1.88
this.jupiterAge = 0; //11.86
this.averageLife = 65;
}
lifeExpOnMars() {
if (this.earthAge > this.averageLife) {
let surpassedYears = (this.earthAge - this.averageLife) * 1.88;
return Math.floor(surpassedYears);
} else {
let remainingYears = this.averageLife - this.earthAge;
return Math.floor(remainingYears * 1.88);
}
}
}
My first return statement in the function doesn't show Jest coverage because that code isn't being run because of my input passed through the first if statement isn't true. The second return statement is showing as covered because that's the code actually being returned since the first if statement is false based on my input earthAge being less than the averageLife.
I need help to figure out I can write a test that covers the first return statement since it's doesn't need to be executed because the condition isn't met.
Tests for Reference:
describe('Galactic', function(){
let galacticAge;
beforeEach(function(){
galacticAge = new Galactic(25)
test('return life expectancy on Mars', function(){
expect(galacticAge.lifeExpOnMars()).toEqual(75);
});
Instantiate the Galactic
with different parameters in each test case. Let the if
condition statement becomes truthy.
index.js
:
export class Galactic {
constructor(earthAge) {
this.earthAge = earthAge;
this.mercuryAge = 0; //.24 earth years
this.venusAge = 0; //.62
this.marsAge = 0; //1.88
this.jupiterAge = 0; //11.86
this.averageLife = 65;
}
lifeExpOnMars() {
if (this.earthAge > this.averageLife) {
let surpassedYears = (this.earthAge - this.averageLife) * 1.88;
return Math.floor(surpassedYears);
} else {
let remainingYears = this.averageLife - this.earthAge;
return Math.floor(remainingYears * 1.88);
}
}
}
index.test.js
:
import { Galactic } from './';
describe('68264949', () => {
it('should cover if branch', () => {
const galactic = new Galactic(100);
const actual = galactic.lifeExpOnMars();
expect(actual).toEqual(65);
});
it('should cover else branch', () => {
const galactic = new Galactic(25);
const actual = galactic.lifeExpOnMars();
expect(actual).toEqual(75);
});
});
unit test result:
PASS examples/68264949/index.test.js (10.509 s)
68264949
✓ should cover if branch (2 ms)
✓ should cover else branch
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 12.1 s
Ran all test suites related to changed files.