istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

How to ignore default value branch for ES6

Open mspoulsen opened this issue 8 years ago • 12 comments

I have same problem as Sabrina describes here:

http://stackoverflow.com/questions/36929320/istanbul-how-to-ignore-default-value-branch-for-es6-babel-compiles-to-es5

Anybody know the answer?

Thanks!

mspoulsen avatar Jul 13 '16 08:07 mspoulsen

You can cover the default value branch by writing a test case where in you don't pass the value.

arjunu avatar Sep 16 '16 07:09 arjunu

I have the same issue. My coverage report for a file looks like this:

JSFiddle with HTML Cov Report

I added the report and my spec file to the fiddle. The report says "29/32" branches. But I'm calling my functions with any combination of parameters.

Chaoste avatar Oct 07 '16 07:10 Chaoste

Per https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignore-default-assignments

var object = parameter || /* istanbul ignore next: tired of writing tests */ {};

mrchief avatar Jan 24 '17 20:01 mrchief

@arjunu, In the case when the default opens a socket or writes to a file or mutates a database or performs some other side effect, not passing an argument is not always an option.

joelnet avatar Sep 09 '17 00:09 joelnet

@mrchief this does not work for es6 assignments constructor(private queryClass: typeof RedisQuery /* istanbul ignore next: tired of writing tests */ = RedisQuery, private redisClass: typeof RedisClient = RedisClient) { has no effect

dcharbonnier avatar Oct 10 '17 08:10 dcharbonnier

@dcharbonnier This issue is specifically about ignoring a default branch. Your use case is different. Also, seems like you're using typescript? If so, then you're further away from ES6 and transpiler might be screwing things up during transpilation. I don't know for a fact, just guessing.

The point is, I know that trick works for default branches, arrow functions, try/catch blocks as I'm using it in various pieces of code in several projects.

That said, would love the contributors to chime in here. It may help to have this feature but then again, its a call that they have to make.

mrchief avatar Oct 10 '17 12:10 mrchief

I've been wrapping my head around how to ignore these. None of the following work (inside a list of params or object destructuring):

/* istanbul ignore next */ sourcesArchivePath = DEFAULT_SOURCES_ARCHIVE_PATH

sourcesArchivePath = /* istanbul ignore next */ DEFAULT_SOURCES_ARCHIVE_PATH

/* istanbul ignore next */ 
sourcesArchivePath = DEFAULT_SOURCES_ARCHIVE_PATH

felixfbecker avatar Aug 05 '18 02:08 felixfbecker

For those using typescript: make sure tsc is not set to remove comments ("removeComments": true in tsconfig.json), else istanbul won't find them (duh).

elialgranti avatar Aug 10 '18 06:08 elialgranti

This is the best i can do to avoid the "If path not taken" message. This feels like a defeat because i have tested this with all variety of parameters including leaving it out, but nothing causes the message to go away except ignoring it. I would love to be proven wrong on this:

private myPrivateFunction(
  /* istanbul ignore next: because TypeScript default assignments are untestable i guess? */
  myVar: boolean = false
) {
  return doStuff(myVar);
}

myPublicFunction(anotherVar: boolean = false) {
  return myPrivateFunction(anotherVar);
}

jeremymoritz avatar Aug 25 '18 12:08 jeremymoritz

I'm answering my own question. As it turns out, the problem is that the function i was trying to test was a private function, and the only way i was reaching it was through other public functions. These public functions set the default value of myVar to false, but then they were always passing the value to my private method (i.e. never ignoring it for the private method to set its default. I'm so happy to say that Istanbul was right all along! Thank you, @gotwarlost for all the help your docs and comments have provided!

jeremymoritz avatar Aug 25 '18 12:08 jeremymoritz

As stated in one of the answers on Stack Overflow, this might work

function(
  /* istanbul ignore next */
  a = 123
){

}

msafi avatar Feb 10 '20 01:02 msafi

This may be happening to you if you are transpiling to ES5, because even if you preserve your comments they may end up in a different place.

One option, if possible for you, may be to target ES6 or higher.

If you are using Typescript, then update your tsconfig.json to have { "compilerOptions": { "target": "ES6" } }, etc.

Once done, you should find that the following works as expected:

function myFunc(
  requiredProp: string,
  /* istanbul ignore next */
  optionalProp = 'defaultValue',
) {
  /* ... */
}

NB - I took the hint from this post https://github.com/microsoft/TypeScript/issues/13029#issuecomment-287901266

pete-otaqui-sky avatar Mar 11 '21 16:03 pete-otaqui-sky