Hone logo
Hone
Problems

Achieving 100% Statement Coverage with Jest in a TypeScript Utility Function

This challenge focuses on writing a simple TypeScript utility function and then crafting Jest tests to achieve 100% statement coverage. Statement coverage is a fundamental metric in software testing, ensuring that every line of code in your function is executed at least once during testing. This exercise will reinforce your understanding of Jest, TypeScript, and the importance of comprehensive test suites.

Problem Description

You are tasked with creating a TypeScript utility function called safeParseInt that attempts to parse a string into an integer. If the string can be successfully parsed as an integer, the function should return the integer value. If the string cannot be parsed as an integer (e.g., it's empty, contains non-numeric characters, or is null/undefined), the function should return null.

Your goal is to write Jest tests that ensure 100% statement coverage of the safeParseInt function. This means every line of code within the function must be executed by at least one test case. Consider various input scenarios, including valid integers, invalid strings, empty strings, null, and undefined.

Key Requirements:

  • The safeParseInt function must be written in TypeScript.
  • The function must handle both successful and unsuccessful parsing scenarios.
  • The Jest tests must achieve 100% statement coverage.
  • Tests should be clear, concise, and well-documented.

Expected Behavior:

  • safeParseInt("123") should return 123.
  • safeParseInt("abc") should return null.
  • safeParseInt("") should return null.
  • safeParseInt(null) should return null.
  • safeParseInt(undefined) should return null.
  • safeParseInt("123.45") should return null.
  • safeParseInt("-123") should return -123.
  • safeParseInt("0") should return 0.

Examples

Example 1:

Input: safeParseInt("42")
Output: 42
Explanation: The string "42" is a valid integer and can be successfully parsed.

Example 2:

Input: safeParseInt("hello")
Output: null
Explanation: The string "hello" is not a valid integer and cannot be parsed.

Example 3:

Input: safeParseInt(null)
Output: null
Explanation: The input is null, which is not a valid string representation of an integer.

Constraints

  • The safeParseInt function should be concise and efficient.
  • The Jest tests should be written using best practices for readability and maintainability.
  • The solution must be in TypeScript.
  • You are free to use any standard Jest features.

Notes

  • Statement coverage focuses on executing every line of code. Branch coverage (executing all possible branches in if statements, etc.) is a separate, but related, metric. This challenge focuses solely on statement coverage.
  • Consider using parseInt with a radix of 10 to ensure consistent parsing.
  • Think about how to test both the success and failure paths of the function. A good test suite will cover all possible scenarios.
  • Use Jest's reporting features to verify that you have achieved 100% statement coverage. npm test --coverage is a common command.
Loading editor...
typescript