Search code examples
reactjsunit-testingtestingjestjsglob

How can I exclude files from code coverage from the beginning of the filename


Using Jest, I want to exclude files from coverage according to the beginning of their filename, regardless of the depth with which the file sits in a directory structure.

Let's say I have something like this:

  • src
    • form
      • controls
        • lookups
          • Lookup.component.js
          • FormLookup.component.js
        • input
          • Input.component.js
          • FormInput.component.js

I would want to exclude any component file beginning with Form from coverage. Something like

coveragePathIgnorePatterns: [
  'src/form/controls/**/*(^Form)*.component.js'
]

That doesn't work, but hopefully you get the gist of what it is I'm trying to accomplish


Solution

  • I use the not (!) operator on the collectCoverageFrom setting, and this seems to work for me:

    collectCoverageFrom: [
        '<rootDir>/**/*.ts',
        '!<rootDir>/**/*.interface.ts',
        '!<rootDir>/**/*.mock.ts',
        '!<rootDir>/src/main.ts',
        '!<rootDir>/src/library/base/abstract-fetch-data-service.ts',
    

    In that case, I believe you can simply add a line:

    '!<rootDir>/src/form/controls/**/form*.component.js',