Hone logo
Hone
Problems

Jest Coverage Maps: Visualizing Your Test Coverage

Understanding how well your codebase is tested is crucial for maintaining quality and preventing regressions. Jest, a popular JavaScript testing framework, provides built-in coverage reporting that can be visualized as HTML reports. This challenge focuses on configuring and interpreting these coverage maps to ensure comprehensive testing.

Problem Description

Your task is to configure Jest to generate an HTML coverage report for a provided TypeScript project. You will then be expected to analyze this report to identify areas of your code that are not adequately covered by tests. You will need to ensure that specific files or directories are included or excluded from coverage reporting based on project needs.

Key Requirements:

  • Configure Jest to output an HTML coverage report.
  • Include all TypeScript files within the src directory in the coverage report.
  • Exclude the test directory (where your test files reside) from the coverage report.
  • Analyze the generated report to identify at least one function or line that is not covered by any test.
  • Write a new test case that specifically covers a previously uncovered part of the code.
  • Regenerate the coverage report to confirm that the newly added test has improved coverage for the targeted area.

Expected Behavior:

Upon running jest --coverage, an coverage/ directory should be generated, containing an index.html file. Opening this file in a web browser should display a detailed coverage report. This report should show overall coverage percentages and allow you to drill down into individual files to see line-by-line coverage.

Edge Cases to Consider:

  • What happens if a file is present but has no tests that execute any of its lines?
  • How does Jest handle syntax errors in files (though this is less about coverage configuration and more about Jest's parsing)?

Examples

Example 1: Basic Coverage Generation

  • Input: A standard Jest setup in a TypeScript project with some source files and tests.
  • Output: A generated coverage/ directory with index.html. The index.html would show overall coverage percentages for statements, branches, functions, and lines.
  • Explanation: Running jest --coverage with default settings will produce a basic coverage report.

Example 2: Excluding Specific Files/Directories

  • Input: A TypeScript project with src/utils.ts and test/utils.test.ts. The test directory should be excluded from coverage.
  • Configuration (jest.config.ts):
    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node',
      collectCoverage: true,
      coverageDirectory: 'coverage',
      coveragePathIgnorePatterns: [
        "/node_modules/",
        "/test/" // Exclude the test directory
      ],
      moduleFileExtensions: [
        "ts",
        "js",
        "json",
        "node"
      ],
      transform: {
        "^.+\\.ts$": "ts-jest"
      },
      testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$"
    };
    
  • Output: The coverage/index.html report will show coverage for src/utils.ts but will not include any metrics for files within the test/ directory.
  • Explanation: The coveragePathIgnorePatterns configuration explicitly tells Jest to ignore paths matching the provided regular expressions.

Example 3: Identifying and Covering Uncovered Lines

  • Scenario: You have a function calculateDiscount(price: number, discountRate: number): number in src/pricing.ts. Currently, your tests only cover the case where discountRate is 0. There is a conditional check for discountRate > 0 that is never executed.
  • Initial Coverage Report: The line-by-line report for src/pricing.ts shows the conditional line as uncovered.
  • Action: Add a new test case to test/pricing.test.ts that calls calculateDiscount with a positive discountRate.
  • Regenerated Coverage Report: The line-by-line report for src/pricing.ts now shows the previously uncovered conditional line as covered.
  • Explanation: By analyzing the report and adding a targeted test, you have successfully improved code coverage.

Constraints

  • The project will be a standard TypeScript project using ts-jest and jest.
  • You will be provided with a basic project structure containing a few TypeScript source files and corresponding test files.
  • Your final coverage report should aim for at least 80% line coverage for files within the src directory.
  • The coverage report must be generated in HTML format.

Notes

  • Consult the official Jest documentation for details on configuring coverage: https://jestjs.io/docs/configuration#collectcoverage-boolean and https://jestjs.io/docs/configuration#coveragepathignorepatterns-arraystring.
  • Pay close attention to the collectCoverage, coverageDirectory, and coveragePathIgnorePatterns options in your jest.config.ts (or equivalent).
  • Line coverage is a good starting point, but also consider what branch and function coverage imply about the thoroughness of your tests.
  • The goal is not just to generate a report, but to understand how to use it to guide your testing efforts.
Loading editor...
typescript