Skip to content Skip to sidebar Skip to footer

Jest Coverage In A Node Typescript Project Always Returns Empty

I am working on backend Typescript project when i am trying to get coverage report for unit test case,Jest returns empty coverage report in terminal as well as in html report stati

Solution 1:

Can able to fix this issue by moving config file to root, Everything is good here in configuration, I dont know why jest behaves like this.

Updated structure

Folder Structure

|-- app
    |-- controllers
    |-- schemas
|-- jest_unit.config.js
|-- package.json
|-- tests
    |-- api
        |-- modules
            |-- m1
                |-- controllers
                    |-- m1_controller_unit.test.ts
                    |-- m1_controller_integration.test.ts
            |-- m2
                |-- models
                    |-- m1_model_unit.test.ts
                    |-- m1_model_integration.test.ts
            |-- m3
                |-- schemas
                    |-- m1_schema_unit.test.ts
                    |-- m1_schema_integration.test.ts

package.json

"scripts":{"unit-test":"jest --config='./jest_unit.config.js' --forceExit --detectOpenHandles",}

Solution 2:

Try changing your collectCoverageFrom as follows:

  collectCoverageFrom: [
    '../../tests/**'
  ],

The ** means to include all files, recursively. Details are at https://jestjs.io/docs/en/configuration#collectcoveragefrom-array

Solution 3:

I'm also facing that issue randomly, I don't have a fix or know why, but clearing the cache and running it again always works:

jest --clearCache

jest --coverage

I believe this might be related to ts-jest not jest itself.

Solution 4:

I got it working with this

package.json

...
"scripts": {
        ...
        "test": "jest --maxWorkers=1 --coverage"
...

jest.config.js

module.exports = {
    verbose: true,
    preset: 'ts-jest',
    testEnvironment: 'node',
    globals: {
        'ts-jest': {
            isolatedModules: true
        }
    },
    testPathIgnorePatterns: ['.d.ts', '.js'],
    collectCoverageFrom: [
        '**/*.ts',
        '!**/build/**',
        '!**/node_modules/**',
        '!**/vendor/**'
    ]
};

Post a Comment for "Jest Coverage In A Node Typescript Project Always Returns Empty"