ezdin gharbi
ezdin gharbi

Reputation: 19

unit test for a line of code

I'm new at writing unit tests and I'm writing unit tests for my bot code. I have this part bothering me :

const accessToken = (() => {
  if (process.argv.length !== 3) {
    console.log('usage: node main/implementation');
    process.exit(1);
  }
  return process.argv[2];
})();

istanbul is showing me that this line return process.argv[2]; is not covered, but I don't have any idea about writing a unit test for that line. Any idea ?

Upvotes: 0

Views: 500

Answers (1)

ThomasThiebaud
ThomasThiebaud

Reputation: 11969

If istanbul says that this line is not covered, it means that all your tests cases match

process.argv.length !== 3

and so none of your test is going to this line

return process.argv[2];

Before fixing it, you should ask yourself if it is relevant to add a test for this line (100% coverage is not always necessary).

If you want to fix it, maybe you can try to set a value of process.argv. (This is just an idea, I'm not sure you can set the value of process.argv like this). Here is a pseudo-code

const fakeArgv = [1, 2, 3];
process.argv = fakeArgv;

it('should return third argv', () => {
  expect(accessToken).to.equal(3)
})

Upvotes: 1

Related Questions