Implement toBeLessThan Matcher for Jest
Jest is a powerful JavaScript testing framework that provides a rich set of matchers for asserting expected outcomes. A common assertion is checking if a received value is less than a certain expected value. This challenge asks you to implement a custom Jest matcher, toBeLessThan, to fulfill this exact need. This exercise will deepen your understanding of Jest's custom matcher API and how to create reusable testing utilities.
Problem Description
Your task is to create a custom Jest matcher named toBeLessThan. This matcher should be used within Jest tests to assert that a received numerical value is strictly less than an expected numerical value.
Key Requirements:
- The matcher should be named
toBeLessThan. - It should accept one argument: the
expectedvalue. - It should compare the
receivedvalue (provided by Jest) with theexpectedvalue. - The assertion should pass if
received < expected. - The assertion should fail if
received >= expected. - The matcher should handle non-numerical inputs gracefully (e.g., by failing the assertion with an informative message).
Expected Behavior:
When expect(received).toBeLessThan(expected) is called:
- If
receivedis a number andreceived < expected, the test passes. - If
receivedis a number andreceived >= expected, the test fails with a message like "Expected <received> to be less than <expected>". - If
receivedis not a number orexpectedis not a number, the test should fail with an informative error message.
Edge Cases to Consider:
- Comparing with
Infinityand-Infinity. - Comparing with
NaN. - Cases where
receivedorexpectedare not numbers.
Examples
Example 1:
// In a Jest test file:
test('should pass when received is less than expected', () => {
expect(5).toBeLessThan(10);
});
Output (if test passes): The test passes.
Explanation: 5 is indeed less than 10.
Example 2:
// In a Jest test file:
test('should fail when received is greater than or equal to expected', () => {
expect(10).toBeLessThan(5);
});
Output (if test fails):
Expected 10 to be less than 5.
Explanation: 10 is not less than 5.
Example 3:
// In a Jest test file:
test('should fail when received is equal to expected', () => {
expect(7).toBeLessThan(7);
});
Output (if test fails):
Expected 7 to be less than 7.
Explanation: 7 is not strictly less than 7.
Example 4 (Edge Case - Non-numerical input):
// In a Jest test file:
test('should fail with non-numerical received value', () => {
expect('hello').toBeLessThan(10);
});
Output (if test fails):
Expected 'hello' to be a number.
Explanation: The received value is a string, not a number, so the matcher fails.
Constraints
- The
expectedvalue passed totoBeLessThancan be any JavaScript value, but the matcher will specifically check if it's a finite number for comparison. - The
receivedvalue (fromexpect()) will also be compared, and it must be a number for the primary comparison to occur. - Performance is not a critical concern for this specific matcher implementation, but it should be efficient.
Notes
- You will need to extend Jest's
Matchersinterface to include your custom matcher for proper TypeScript typing. - Consider using Jest's
this.utilsfor messages and other utilities. - Think about how to handle
NaN,Infinity, and-Infinityin your comparisons. - The core logic will involve checking the types of
receivedandexpectedbefore performing the numerical comparison.