Hone logo
Hone
Problems

Achieving Comprehensive Function Coverage with Jest

In software development, ensuring that all parts of your code are tested is crucial for maintaining quality and preventing regressions. Function coverage, a metric that tracks which functions have been executed by your tests, is a key indicator of test suite completeness. This challenge will guide you through understanding and achieving high function coverage using Jest.

Problem Description

Your task is to write a simple TypeScript function and then create a Jest test suite that achieves 100% function coverage for that function. You will need to identify different execution paths within your function and write tests that specifically target each of these paths. Understanding how Jest reports coverage will be a core part of this challenge.

What needs to be achieved:

  1. Implement a TypeScript function with at least two distinct execution paths.
  2. Write a Jest test suite for this function.
  3. Configure Jest to report function coverage.
  4. Ensure your test suite achieves 100% function coverage.

Key requirements:

  • The function should accept at least one argument and have conditional logic (e.g., if, else, switch, ternary operator) that creates different execution paths.
  • Tests should be written in TypeScript.
  • Jest should be used as the testing framework.
  • The output from Jest's coverage report should demonstrate 100% function coverage for your implemented function.

Expected behavior: When jest --coverage is executed, the coverage report should indicate that all functions within your target file have been called by your tests.

Edge cases to consider:

  • Functions with no arguments.
  • Functions with optional arguments.
  • Functions with default parameter values.
  • Functions that might throw errors.

Examples

Let's consider a simple calculator function.

Example 1: Basic Addition

// src/calculator.ts
export function add(a: number, b: number): number {
  return a + b;
}

If you only write a test for add(2, 3), your add function will have 100% function coverage, as there's only one execution path.

Example 2: Conditional Logic

// src/conditionalMath.ts
export function processNumber(num: number): string {
  if (num > 10) {
    return "Large";
  } else if (num < 0) {
    return "Negative";
  } else {
    return "Medium";
  }
}

To achieve 100% function coverage for processNumber, you would need to test inputs that trigger each of the three return statements:

  • A number greater than 10 (e.g., 15)
  • A number less than 0 (e.g., -5)
  • A number between 0 and 10 (inclusive) (e.g., 5)

Constraints

  • The target TypeScript file for coverage analysis should not exceed 50 lines of code (excluding comments and blank lines).
  • Your Jest test file should not exceed 30 lines of code.
  • The function should be exported from a module.
  • You are expected to use standard Jest configuration or minimal configuration if needed to enable coverage.

Notes

  • To enable coverage reporting, you will typically run Jest with the --coverage flag (e.g., jest --coverage).
  • Consider how different inputs can lead your function down different code paths.
  • Think about what "function coverage" specifically means in the context of your test runs.
  • You may need to set up a basic Jest configuration in your jest.config.js or package.json to ensure TypeScript files are processed correctly for coverage. Hints: look into transform and collectCoverageFrom options in Jest configuration.
Loading editor...
typescript