Get #Amazon #Prime for this #holiday #amazonprime #christmas #2019

How to ignore a section of #JavaScript with #istanbul test coverage?

Sometimes, part of the code is hard to test. For example, I have this section of code to export my class so that it can be attached to window object when it is used on a browser.

For example

class MyClass {
  ...
}    

if (typeof window !== 'undefined'
  && typeof window.MyClass === 'undefined') {
  window.MyClass = MyClass;
}

How to achieve 100% code coverage using Istanbul if I can't really write unit tests for this section of code?

Istanbul

Istanbul Homepage
Istanbul Homepage

Taken from Istanbul GitHub page, Istanbul is a JavaScript code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests.

Check out Istanbul ignoring code for coverage purpose document on GitHub to find out more examples on how to use it.

You can do the following to ignore part of the code for test coverage purpose.


class MyClass {
  ...
}    

/* istanbul ignore next */
if (typeof window !== 'undefined'
  && typeof window.MyClass === 'undefined') {
  window.MyClass = MyClass;
}

There are these instructions


/* istanbul ignore next */
/* istanbul ignore if */
/* istanbul ignore else */

Thanks for reading!

Jun