Testing a Simple Utility Function with Jest
This challenge focuses on writing a comprehensive test suite for a basic TypeScript utility function using Jest. Testing is a crucial part of software development, ensuring code reliability and preventing regressions. You'll practice writing various test cases, including positive, negative, and edge cases, to thoroughly validate the function's behavior.
Problem Description
You are given a TypeScript function capitalize that takes a string as input and returns a new string with the first letter capitalized. Your task is to implement a Jest test suite to verify the correctness of this function. The test suite should cover various scenarios, including:
- Positive Cases: Capitalizing strings with lowercase first letters.
- Negative Cases: Handling empty strings and strings with no letters.
- Edge Cases: Strings with already capitalized first letters, strings with special characters, and strings with numbers.
The test suite should include at least 5 test cases, each clearly describing the scenario being tested. Each test case should assert that the function returns the expected output for the given input.
Examples
Example 1:
Input: "hello"
Output: "Hello"
Explanation: The first letter 'h' is converted to 'H'.
Example 2:
Input: ""
Output: ""
Explanation: An empty string remains an empty string.
Example 3:
Input: "WORLD"
Output: "WORLD"
Explanation: The string already starts with a capital letter, so it remains unchanged.
Example 4:
Input: "123abc"
Output: "123abc"
Explanation: The first character is a number, so it remains unchanged.
Example 5:
Input: "!@#test"
Output: "!@#test"
Explanation: The first character is a special character, so it remains unchanged.
Constraints
- The
capitalizefunction is provided below. You are not allowed to modify it. - You must use Jest for testing.
- The test suite must be written in TypeScript.
- All test cases must pass without errors.
- The test suite should be well-structured and readable.
Notes
- Consider using
expect.toBefor strict equality checks. - Think about different input types and boundary conditions to ensure thorough testing.
- Use descriptive test names to clearly indicate the purpose of each test case.
- The
capitalizefunction is defined as follows:
function capitalize(str: string): string {
if (!str) {
return "";
}
return str.charAt(0).toUpperCase() + str.slice(1);
}