Jest: Ignore lines for code coverage

北慕城南 提交于 2019-11-29 10:48:46

问题


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.


回答1:


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




回答2:


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




回答3:


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

class Foo {
  bar /* istanbul ignore next */ () {
    return 'biu~';
  }
}



回答4:


For TypeScript users:

There seems to be an issue here with this not working with ts-jest



来源:https://stackoverflow.com/questions/38740165/jest-ignore-lines-for-code-coverage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!