Swapnil Kotkar
Swapnil Kotkar

Reputation: 161

jest --coverage not generating report

I want to put some functions (sum, mul, sub, div) in the test file just like below,

import {describe, expect} from "@jest/globals";

function sum(a,b) {
    return a+b;
}

const div = (a,b) => {
    return a/b;
}

const mul = (a,b) => {
    return a*b;
}

const sub = (a,b) => {
    return a-b;
}

describe('testing', ()=> {
    
    it('test', ()=> {
        expect(sum(1,2)).toBe(3);
    });

    it('test2', ()=> {
        expect(mul(2,2)).toBe(4);
    });
});

When I hit the command npm test -- --coverage, it will generate report just like below, enter image description here

If you see in report in above image, both test cases are passing but table shows 0 in every column, that means it is not detecting those functions.

What to do here?

Note: I don't want to shift those functions (sum, mul, sub, div) outside the file. I want to keep those functions in that test file only.

Upvotes: 1

Views: 1252

Answers (1)

Емил Цоков
Емил Цоков

Reputation: 692

You are after forceCoverageMatch jest config option

From jest docs: "Test files are normally ignored from collecting code coverage. With this option, you can overwrite this behavior and include otherwise ignored files in code coverage."

import type {Config} from 'jest';

const config: Config = {
  forceCoverageMatch: ['**/*.t.js'],
};

export default config;

https://jestjs.io/docs/configuration

Upvotes: 1

Related Questions