Testing Regular Expressions with toMatch in Jest
This challenge focuses on writing Jest tests that utilize the toMatch matcher to verify that a string matches a given regular expression. Regular expressions are powerful tools for pattern matching, and Jest's toMatch provides a convenient way to assert that a string conforms to a specific regex pattern. This is crucial for validating user input, data formats, and other scenarios where pattern matching is required.
Problem Description
You are tasked with creating a Jest test suite to verify the functionality of a function called validateInput. This function takes a string as input and returns true if the string matches a specific regular expression, and false otherwise. Your goal is to write Jest tests using the toMatch matcher to ensure that validateInput behaves as expected for various inputs, including valid and invalid cases.
What needs to be achieved:
- Write a Jest test suite for the
validateInputfunction. - Use the
toMatchmatcher to assert that the function returns the correct boolean value based on whether the input string matches the defined regular expression. - Cover both positive (valid input) and negative (invalid input) test cases.
Key requirements:
- The regular expression to be used is
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/. This regex is intended to validate email addresses. - The
validateInputfunction is already provided (see below). You should not modify this function. - Your tests should be clear, concise, and well-documented.
Expected behavior:
The tests should pass if the validateInput function correctly validates email addresses according to the provided regular expression. Tests should fail if the function returns an incorrect boolean value for any given input.
Edge cases to consider:
- Empty strings
- Strings with invalid characters (e.g., spaces in the email address)
- Strings with missing parts (e.g., missing "@" or ".")
- Valid email addresses with different domain extensions (e.g., .com, .org, .net)
- Valid email addresses with special characters in the username (e.g., using "+" or "_")
Examples
Example 1:
Input: validateInput("test@example.com")
Output: true
Explanation: The input string is a valid email address and matches the regex.
Example 2:
Input: validateInput("invalid email")
Output: false
Explanation: The input string is not a valid email address and does not match the regex.
Example 3:
Input: validateInput("")
Output: false
Explanation: An empty string is not a valid email address and does not match the regex.
Constraints
- The regular expression
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/must be used. - The
validateInputfunction cannot be modified. - All tests must be written in TypeScript.
- The test suite should include at least 5 test cases, covering both valid and invalid email addresses.
Notes
- The
toMatchmatcher in Jest is designed to compare a string against a regular expression. - Consider using descriptive test names to clearly indicate what each test case is verifying.
- Think about the different ways an email address can be invalid and create tests to cover those scenarios.
- The provided
validateInputfunction is a simple implementation and does not handle all possible email address validation scenarios. The focus is on testing its behavior against the given regex.
// Provided function - DO NOT MODIFY
function validateInput(input: string): boolean {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return regex.test(input);
}