Robust Regression Detection in Jest
Regression testing is crucial for maintaining software quality. This challenge asks you to implement a Jest test suite that automatically detects regressions in a simple function. You'll be provided with a baseline set of inputs and expected outputs, and your task is to create tests that flag any deviations from this baseline after a code change.
Problem Description
You are given a function add that performs a simple addition operation. Your goal is to create a Jest test suite that verifies the function's behavior against a predefined baseline. The test suite should:
- Establish a Baseline: The baseline consists of a set of input-output pairs. These pairs represent the expected behavior of the
addfunction. - Detect Regressions: After any modification to the
addfunction, the test suite should automatically detect if the function's output for any of the baseline inputs has changed. A regression is flagged if the actual output differs from the expected output. - Provide Clear Failure Messages: When a regression is detected, the test suite should provide a clear and informative failure message indicating the input that caused the regression and the expected vs. actual output.
- Handle Edge Cases: Consider cases where the inputs might be zero or negative.
The add function is defined as follows:
function add(a: number, b: number): number {
return a + b;
}
Examples
Example 1:
Input: add(2, 3)
Output: 5
Explanation: The function should correctly add 2 and 3.
Example 2:
Input: add(-1, 1)
Output: 0
Explanation: The function should handle negative numbers correctly.
Example 3:
Input: add(0, 0)
Output: 0
Explanation: The function should handle zero inputs correctly.
Constraints
- The
addfunction will only accept numeric inputs (integers or floating-point numbers). - The test suite should cover at least the examples provided above, plus at least three additional test cases with different combinations of positive, negative, and zero inputs.
- The test suite should be written in TypeScript and use Jest.
- The test suite should be designed to be easily extensible to accommodate more baseline inputs in the future.
- The test suite should be robust and not prone to false positives (i.e., flagging changes that are not actual regressions).
Notes
- Consider using a data-driven testing approach (e.g.,
test.each) in Jest to efficiently test multiple input-output pairs. - Focus on creating a clear and maintainable test suite that effectively detects regressions.
- Think about how to structure your test cases to ensure good code coverage.
- The baseline data should be hardcoded within the test suite for simplicity. No external data files are required.
- The goal is to detect any deviation from the baseline, not to optimize for performance. Readability and maintainability are prioritized.