Jest Coverage Directory Configuration
This challenge focuses on configuring Jest to generate code coverage reports in a specific directory. Properly configured coverage reports are essential for understanding the thoroughness of your test suite and identifying areas that need more testing.
Problem Description
Your task is to configure Jest, a popular JavaScript testing framework, to output its code coverage reports into a custom directory instead of the default location. You will need to modify the Jest configuration file to achieve this.
What needs to be achieved:
- Jest should generate code coverage reports.
- The generated coverage reports must be placed in a directory named
custom-coverageat the root of your project.
Key requirements:
- Use TypeScript for your project.
- Ensure Jest is configured to run tests and generate coverage.
- Modify the Jest configuration to point the coverage directory to
custom-coverage.
Expected behavior:
When you run the Jest command with coverage enabled (e.g., jest --coverage), a directory named custom-coverage should be created in your project's root. Inside this directory, you will find the HTML, JSON, and LCOV coverage reports.
Edge cases to consider:
- What happens if the
custom-coveragedirectory already exists? (Jest should overwrite or update its contents). - What if no tests are found? (Coverage report generation should still attempt to run, but may be empty or indicate no tests were run).
Examples
Example 1: Assume you have a project with the following structure:
my-project/
├── src/
│ └── index.ts
├── tests/
│ └── index.test.ts
├── package.json
└── jest.config.ts
And jest.config.ts contains a basic Jest configuration.
Input (Command to run):
npm test -- --coverage
# or
yarn test --coverage
Expected Output (Directory Structure after running the command):
my-project/
├── src/
│ └── index.ts
├── tests/
│ └── index.test.ts
├── custom-coverage/ <-- This directory should be created
│ ├── index.html
│ ├── coverage.json
│ └── lcov.info
├── package.json
└── jest.config.ts
Explanation:
By configuring Jest correctly, running the coverage command generates the reports within the newly created custom-coverage directory.
Example 2:
If jest.config.ts is not yet configured for coverage output directory:
Input (Content of jest.config.ts):
import type { JestConfigWithTsJest } from 'ts-jest';
const config: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'node',
};
export default config;
Command to run:
npm test -- --coverage
Expected Output (Directory Structure after running the command):
A coverage directory (Jest's default) would be created, not custom-coverage. This is the behavior we aim to change.
Constraints
- Your project must be set up to use TypeScript and Jest.
- The Jest configuration file should be named
jest.config.ts. - The custom coverage directory must be named exactly
custom-coverage. - The solution should focus solely on the Jest configuration. Assume Jest and
ts-jestare already installed and functional.
Notes
- You will need to consult the Jest documentation for configuration options related to code coverage.
- Pay close attention to the
coverageDirectoryoption within the Jest configuration. - Ensure your
package.jsonhas atestscript that runs Jest with the--coverageflag, or you can invoke Jest directly with this flag.