3
votes

When I run code coverage, code inside the root directory containing jest config works perfectly but for the code outside the root directory test cases passes but in the coverage report it shows zero percentage for all the test files outside the root directory.

jest.config.js file is inside explorebook folder.

directory structure:

|---core
|   |--components
|      |--test
|   |--jest.setup.suites.js
|---explorebook
|    |--components
|    |  |--test
|    |--package.json
|    |--jest.config.js
|    |--jest.setup.suites.js
|---framework    
|     |--commons
|        |--test
|     |--jest.setup.suites.js 
|

jest.config.js

const TEST_FILES_REGEX = '(/test/.*|(\\.|/)(test|spec))\\.js$';

module.exports = {
    verbose: false,
    roots: [
        '<rootDir>/',
        '<rootDir>/../core/',
        '<rootDir>/../framework/commons/'
    ],
    collectCoverage: true,
    collectCoverageFrom: [
         'components/**/*.js',
         '<rootDir>/../core/components/**/*.js'
         '<rootDir>/../framework/commons/*.js'
    ],
    coverageDirectory: 'reports/coverage',
    coverageReporters: [
        'lcov',
        'text'
    ],
   setupFiles: [ '<rootDir>/jest.setup.suites.js', 
          '<rootDir>/../core/jest.setup.suites.js', 
          '<rootDir>/../framework/jest.setup.suites.js' ],
   setupTestFrameworkScriptFile: path.join(__dirname, 'jest.setup.tests.js'),
   testEnvironment: 'jsdom',
   testRegex: TEST_FILES_REGEX

};

coverage report

Even if all the outside the rootDir passes, report does not show that.

I would expect the coverage values for files outside also to be correct in the report.

1
Did you ever solve this?Clint

1 Answers

5
votes

Jest will retrofit code coverage only on files inside the project's rootDir (source). In your setup, this will be the explorebook folder.

To also run coverage on files in a different folder than the one containing jest.config.js, I suggest setting [rootDir] (source) to your project's root folder, and update any necessary paths: jest.config.js

const TEST_FILES_REGEX = '(/test/.*|(\\.|/)(test|spec))\\.js$';

module.exports = {
    verbose: false,
    rootDir: './../' // This should point to your project root folder
    collectCoverage: true,
    collectCoverageFrom: [
         '**/*.js',
    ],
    coverageDirectory: '<rootDir>/explorebook/reports/coverage',
    coverageReporters: [
        'lcov',
        'text'
    ],
   setupFiles: [ '<rootDir>/explorebook/jest.setup.suites.js', 
          '<rootDir>/core/jest.setup.suites.js', 
          '<rootDir>/framework/jest.setup.suites.js' ],
   setupTestFrameworkScriptFile: path.join(__dirname, 'jest.setup.tests.js'),
   testEnvironment: 'jsdom',
   testRegex: TEST_FILES_REGEX

};