Home > database >  Show real test coverage without 'Ignore' annotations in Istanbul
Show real test coverage without 'Ignore' annotations in Istanbul

Time:11-11

Is it possible to show the actually code coverage with Istanbul, ignoring the "ignore" annotations?

e.g. Don't actually ignore next:

/* istanbul ignore next */

Our coverage reports 100% coverage but has ignores in the code.

CodePudding user response:

I realize it is not the answer you want to hear, though for me, it is the only logical answer: Remove these lines from your codebase.

Let me explain why: The only function of these lines is to make sure the following line/statement is not included in the coverage results. So if you don't want it to be ignored, remove the lines.

If, for some reason, you really want to keep the lines in your codebase. You could consider a CI based solution. Create a job that first removes all the lines you want to have removed. Something like this:

for file in *.js
do
    sed '/istanbul ignore/d' "$file" > "$file".new_file.js
    mv "$file".new_file.js "$file".js
done

And then calculate the code coverage as you normally do.

Though, as this will change all files, it will be not ideal to run locally as you probably need to git-revert all changes afterwords. Therefore, I suggested CI, as there you can simply end the job and ignore the changes you made.

CodePudding user response:

Adding a lint rule to prevent usage of /* istanbul ignore ... */, as you suggested in your comment, is probably the best solution.

However, as Istanbul is open source JavaScript software, you could also easily modify it, so that it ignores the /* istanbul ignore ... */ statements. Here is a quick and dirty ad-hoc solution (the proper solution would be to fork the project, modify the source, build and install it):

  1. Locate the Istanbul dependency or dependencies in your project's node_modules folder.
  2. Search for /^\s*istanbul\s ignore\s in the identified folder(s), which is part of the regex pattern, that Istanbul uses to properly identify the ignore comments.
  3. Modify the pattern in the found source file(s), e.g. change istanbul and/or ignore to some other terms, so that the existing /* istanbul ignore ... */ comments in your project source code are no longer matched (or get rid of the entire mechanism, but that might be more difficult and riskier).

I do not recommend this approach. I just want to show, that it can be done. Depending on your specific setup, it may be easy or difficult to implement. I just tried it with an ES 2015 project in VS Code, and it worked fine.

  • Related