Configuration file: Coverage settings

There are 3 ways to control how code coverage is reported in wallaby.js.

Stopping code coverage calculation for a file

Sometimes you may just need to exclude a file from code coverage calculation reported in the Wallaby App, and still be able to see its coverage indicators, inline errors and messages, etc., when the file is opened in your code editor.

You may use the filesWithNoCoverageCalculated setting in these scenarios. For example, the following config will exclude all files that name ends with -helper.js from code coverage calculation:

  module.exports = function () {

    return {
      ...
      filesWithNoCoverageCalculated: ['src/**/*-helper.js']
    };
  };

Wallaby.js also allows to ignore a file for coverage purposes by using comment hints. You may define your own hint by specifying a regular expression or a string, for example:

  module.exports = function () {

    return {
      ...
      hints: {
        ignoreCoverageForFile: /ignore file coverage/
      },
    };
  };

You may then use the comment inside files that you would like to ignore for coverage purposes.

The default value of the ignoreCoverageForFile setting is /ignore file coverage/.

Stopping code coverage calculation for code blocks inside a file

Certain functions, statements or logical branches may be impossible or impractical to unit test. Similar to /* istanbul ignore next */, wallaby.js allows to ignore code for coverage purposes by using comment hints.

The default value of the ignoreCoverage setting is /ignore coverage|istanbul ignore|c8 ignore/.

You may define your own hint by specifying a regular expression or a string, for example:

  module.exports = function () {

    return {
      ...
      hints: {
        // or /istanbul ignore next/, or any RegExp
        ignoreCoverage: /ignore coverage/
      },
    };
  };

You may then use the hint for code blocks/branches that you would like to ignore for coverage purposes:

var object = parameter || /* ignore coverage: should never happen */ {};

if (simpleError ||
    /* ignore coverage */ reallyDifficultToProduceError) {

}
...

someFunMemberFunction: () => {
    // coverage assessed as normal
}

/* ignore coverage */
render() {
    // this whole body would not be factored for code coverage at all
}

Stopping instrumentation for a file

Sometimes you may need to disable code instrumentation for an entire file, you can do this by setting the instrument flag for the file. Please note that you must not exclude your file from being instrumented if you need Wallaby to compile it.

module.exports = function () {
  return {
    files: [
      { pattern: 'src/helper.js', instrument: false },
      ...
    ],

    ...
  };
};

Note that in this case, when the file changes, wallaby.js may not be able to automatically run tests that exercise the code in the file and display inline messages for it, because the tool uses instrumentation to establish the dependencies between source code and tests. So setting the instrument: false flag works best for files that do not normally or frequently change, such as libraries or various helpers/utils.