How to ignore lines for code coverage in Jest
Asked Answered
S

6

84

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.

Severalty answered 3/8, 2016 at 9:51 Comment(0)
P
56

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

Puffy answered 30/5, 2017 at 1:58 Comment(1)
does not work for me. im using react scripts v2.1.5 with jest v23.6.0Caithness
D
38

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

Devaney answered 16/11, 2018 at 13:33 Comment(3)
If using create react app, which installs this plugin for you instead try using collectCoverageFrom and ! to negate a path: "collectCoverageFrom": [ "src/**/*.{js,jsx,ts,tsx}", "!<rootDir>/node_modules/",Laughton
This doesn't work for me even after uninstalling babel-plugin-istanbul, the lines are shown in the coverage (using TS).Garrity
In my case, I had coverageProvider: 'v8', and it was causing this to break. doing: coverageProvider: 'babel', fixed it and the pragma works great.Caporetto
A
16

Just for anyone finding this who uses the v8 provider: the docs now state how to ignore lines with different providers, and link to more details. But basically either /* c8 ignore next */ or /* c8 ignore start */ + /*c8 ignore end */ should work well with the v8 provider.

Example:

/* c8 ignore next */
if (process.env.DEBUG) console.log('debug');

/* c8 ignore start */
switch (process.env.DEBUG) {
case '1':
  console.log('some verbosity');
  break;
case '2':
  console.log('a lot of verbosity');
  break;
}
/* c8 ignore end */
Amylopsin answered 13/5, 2022 at 11:53 Comment(0)
A
13

Found a workaround (spaces before and after the comment seem to be necessary):

class Foo {
  bar /* istanbul ignore next */ () {
    return 'biu~';
  }
}
Audi answered 6/3, 2019 at 6:6 Comment(1)
This works for me. I have to place the comment right in front of the function to be ignored, instead of above it.Jugular
B
1

According to a babel issue thread Istambul appears to have a bug where it assumes the preceding line of code is terminated with a semi-colon...

constructor(message: string) {
  // TODO: how do I get Jest code coverage for "super(message)?
  // /* istanbul ignore next */ assumes the preceding line of code is terminated with a ;
    DEBUG: console.log();
    /* istanbul ignore next */
    super(message);
}
Boffin answered 28/4, 2021 at 6:48 Comment(0)
C
1

In my case, I had coverageProvider: 'v8', and it was causing this to break. doing: coverageProvider: 'babel', fixed it and the pragma works great.

Caporetto answered 6/1, 2022 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.