Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore lines for code coverage in Jest

In Jest, is there any way to ignore code for test coverage? I tried using

/* istanbul ignore next */ 

But it doesn't seem to work.

like image 578
Nahush Farkande Avatar asked Aug 03 '16 09:08

Nahush Farkande


People also ask

How do I ignore Jest coverage?

To ignore a file pattern for Jest code coverage, we can add the option in the Jest config. to calculate the code coverage from the paths that match patterns listed in collectCoverageFrom that don't have the exclamation mark before it. As a result, we see code coverage calculated from the src/**/*.

Does Jest have code coverage?

Jest, a flexible, easy-to-use testing framework, will be used to test a simple Typescript application. Codecov, a tool for monitoring code coverage, will be used to measure the test coverage for your Jest unit tests.

What are uncovered lines in Jest?

Testing uncovered lines These lines are within an if-statement that is only entered when the passed two values together will be greater than 100.

What is Istanbul ignore next?

istanbul is a JavaScript code coverage tool. Some things are hard to test, so /* istanbul ignore next */ tells istanbul to skip and not include the next thing in your source code when calculating code coverage for your project.


2 Answers

It works.

(function(global) {     var defineAsGlobal = true;     /* istanbul ignore next */     if(typeof exports === 'object') {         module.exports = lib;         defineAsGlobal = false;     }     /* istanbul ignore next */     if(typeof modules === 'object' && typeof modules.define === 'function') {         modules.define('lib', function(provide) {             provide(lib);         });         defineAsGlobal = false;     }     /* istanbul ignore next */     if(typeof define === 'function') {         define(function(require, exports, module) {             module.exports = lib;         });         defineAsGlobal = false;     }     /* istanbul ignore next */     defineAsGlobal && (global.lib = lib); })(this); 

Sample project https://github.com/ilyar/sandbox/tree/master/jest

like image 90
ilyar Avatar answered Sep 19 '22 19:09

ilyar


Update for anyone that finds this at a later date.

/* istanbul ignore next */  

Will work but as read from The Jest Official Documentation:

coveragePathIgnorePatterns seems to not have any effect.

Make sure you are not using the babel-plugin-istanbul plugin. Jest wraps Istanbul, and therefore also tells Istanbul what files to instrument with coverage collection. When using babel-plugin-istanbul, every file that is processed by Babel will have coverage collection code, hence it is not being ignored by coveragePathIgnorePatterns.

The documentation can be found here: Documentation

So in order to fix this issue uninstall babel-plugin-istanbul:

If it is a library based only on javascript, than you can just run npm uninstall --save babel-plugin-istanbul or npm uninstall --save-dev babel-plugin-istanbul If you've installed a library with native content that requires linking, and you've linked it with rnpm then you can do: rnpm unlink package_name then follow step 1 - Aakash Sigdel

This quote was from Aakash Sigdel found here: quote

like image 35
Neoxidine Avatar answered Sep 19 '22 19:09

Neoxidine