Hone logo
Hone
Problems

Testing Data Flow with Jest and TypeScript

Data flow coverage ensures that your tests verify the flow of data through your application, from input to output. This challenge focuses on writing Jest tests that specifically target data flow, ensuring that values are correctly transformed and used throughout a simple function. Achieving good data flow coverage helps catch subtle bugs related to incorrect data manipulation.

Problem Description

You are given a TypeScript function processData that takes an initial number, squares it, adds 5, and then returns the result. Your task is to write Jest tests that achieve 100% data flow coverage for this function. This means your tests must cover every possible path the data takes within the function, ensuring that each operation is exercised. You need to write at least two tests to achieve this, covering different input values to ensure all branches of the logic are tested.

Key Requirements:

  • Write Jest tests using TypeScript.
  • Achieve 100% data flow coverage for the processData function.
  • Tests should be clear, concise, and well-documented.
  • Use expect assertions to verify the output of the function.

Expected Behavior:

The processData function should correctly square the input number, add 5, and return the result. Your tests should verify this behavior for different input values, including positive and negative numbers.

Edge Cases to Consider:

  • Zero as input.
  • Negative numbers as input.
  • Large numbers as input (though performance isn't a primary concern here, consider potential overflow if you're feeling ambitious).

Examples

Example 1:

Input: 2
Output: 9
Explanation: 2 * 2 = 4, 4 + 5 = 9

Example 2:

Input: -3
Output: 4
Explanation: -3 * -3 = 9, 9 + 5 = 14

Example 3:

Input: 0
Output: 5
Explanation: 0 * 0 = 0, 0 + 5 = 5

Constraints

  • The processData function is provided below. You should not modify it.
  • You must use Jest and TypeScript.
  • Tests should be written in a separate test file (e.g., processData.test.ts).
  • The focus is on data flow coverage, not performance.

Notes

  • Data flow coverage is about verifying the transformation of data, not just the final result.
  • Consider using different input values to exercise different code paths within the function.
  • Use Jest's coverage reports to verify that you have achieved 100% data flow coverage. You can run jest --coverage to see the coverage report.
  • The provided processData function is intentionally simple to focus on the data flow coverage concept.
// processData.ts
export function processData(input: number): number {
  const squared = input * input;
  const result = squared + 5;
  return result;
}
Loading editor...
typescript