Jest's collectCoverageFrom: Selective Code Coverage
Jest provides powerful features for code coverage analysis, allowing you to understand which parts of your codebase are being tested. Sometimes, you might want to exclude certain files or directories from your coverage reports, perhaps because they are generated code, third-party libraries, or configurations. This challenge focuses on implementing Jest's collectCoverageFrom configuration option to achieve this selective coverage.
Problem Description
Your task is to configure a Jest project to selectively include specific source files in its code coverage reports. You will be given a simple project structure with a few TypeScript files and a Jest configuration file. You need to modify the Jest configuration to ensure that only a designated subset of your source files are considered for coverage.
What needs to be achieved:
Configure jest.config.js to use the collectCoverageFrom option to include only specific files in the coverage report.
Key Requirements:
- Create a basic Jest project with a few dummy TypeScript source files.
- Write a Jest test that will run and generate a coverage report.
- Modify
jest.config.jsto use thecollectCoverageFromarray. - The coverage report should only include coverage data for the files specified in
collectCoverageFrom.
Expected Behavior:
When jest --coverage is executed, the generated coverage report (e.g., in the coverage/lcov-report/index.html) should only list and display coverage metrics for the files explicitly included via collectCoverageFrom. Files not listed in this array, even if they exist in the project, should not appear in the coverage report.
Edge Cases to Consider:
- What happens if
collectCoverageFromis an empty array? - What happens if a file listed in
collectCoverageFromdoes not exist? - How do glob patterns work with
collectCoverageFrom?
Examples
Example 1: Basic Inclusion
Project Structure:
my-project/
├── src/
│ ├── utils.ts
│ ├── calculations.ts
│ └── services.ts
├── __tests__/
│ └── utils.test.ts
└── jest.config.js
src/utils.ts:
export function add(a: number, b: number): number {
return a + b;
}
src/calculations.ts:
export function multiply(a: number, b: number): number {
return a * b;
}
src/services.ts:
export function fetchData(): string {
return "data";
}
__tests__/utils.test.ts:
import { add } from '../src/utils';
describe('add function', () => {
it('should add two numbers correctly', () => {
expect(add(2, 3)).toBe(5);
});
});
jest.config.js (Initial - no collectCoverageFrom):
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
};
Running jest --coverage with the initial config would show coverage for utils.ts, calculations.ts, and services.ts (if tests existed for them or if Jest's default glob patterns include them).
jest.config.js (Modified - targeting utils.ts only):
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
collectCoverageFrom: [
'src/utils.ts'
]
};
Expected Output:
After running jest --coverage with the modified jest.config.js, the coverage report should only show coverage details for src/utils.ts. src/calculations.ts and src/services.ts should not be present in the report.
Example 2: Using Glob Patterns
Project Structure: (Same as Example 1)
jest.config.js (Modified - targeting all files in src/ except services.ts):
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
collectCoverageFrom: [
'src/**/*.ts', // Include all .ts files in src
'!src/services.ts' // Exclude src/services.ts
]
};
Expected Output:
The coverage report should include src/utils.ts and src/calculations.ts, but exclude src/services.ts.
Constraints
- The project should use TypeScript.
- Jest must be configured using
jest.config.js. - The solution must utilize the
collectCoverageFromoption withinjest.config.js. - Tests should be written in TypeScript.
- The code coverage should be generated using the
--coverageflag.
Notes
- You'll need to have
jest,ts-jest, andtypescriptinstalled as development dependencies. collectCoverageFromaccepts an array of glob patterns.- Remember that Jest's default configuration might already include files in coverage. Your goal is to override or specifically define what is included using
collectCoverageFrom. - Consider how to structure your project to make it easy to define exclusions.