Custom Jest Matcher: toBeGreaterThan
Jest's matchers are the core of its assertion capabilities. This challenge asks you to implement a custom Jest matcher called toBeGreaterThan. This matcher will allow you to assert that a value is greater than another value, enhancing the readability and expressiveness of your tests.
Problem Description
You need to create a custom Jest matcher that checks if a value is greater than another value. The matcher should be named toBeGreaterThan. It should accept a single argument, which is the value to compare against. The matcher should return a result object with pass and message properties, indicating whether the assertion passed and a descriptive message explaining the result. The message should be helpful for debugging failed assertions, clearly stating the expected and received values.
Key Requirements:
- Matcher Name: The matcher must be named
toBeGreaterThan. - Single Argument: The matcher must accept a single argument representing the value to compare against.
- Comparison: The matcher must correctly compare the received value with the provided argument using the
>operator. - Result Object: The matcher must return an object with
pass(boolean) andmessage(string) properties. - Descriptive Messages: The messages should clearly indicate whether the assertion passed or failed, and include the expected and received values.
Expected Behavior:
- If the received value is greater than the argument, the matcher should return
{ pass: true, message: () => 'Expected ' + received.toString() + ' to be greater than ' + argument.toString() }. - If the received value is not greater than the argument, the matcher should return
{ pass: false, message: () => 'Expected ' + received.toString() + ' to be greater than ' + argument.toString() }.
Edge Cases to Consider:
- Different Data Types: Handle cases where the received value and the argument are of different data types. While strict type checking isn't required, ensure the comparison behaves reasonably (e.g., comparing a number to a string might lead to unexpected results, but the matcher should still provide a clear message).
- NaN: Consider how
NaNshould be handled.NaN > anyis always false. - Null/Undefined: Consider how
nullandundefinedshould be handled.
Examples
Example 1:
Input: 5 toBeGreaterThan 2
Output: { pass: true, message: () => 'Expected 5 to be greater than 2' }
Explanation: 5 is greater than 2, so the assertion passes.
Example 2:
Input: 2 toBeGreaterThan 5
Output: { pass: false, message: () => 'Expected 2 to be greater than 5' }
Explanation: 2 is not greater than 5, so the assertion fails.
Example 3:
Input: "10" toBeGreaterThan 5
Output: { pass: false, message: () => 'Expected 10 to be greater than 5' }
Explanation: "10" (string) is not greater than 5 (number) in a numerical comparison.
Example 4:
Input: NaN toBeGreaterThan 5
Output: { pass: false, message: () => 'Expected NaN to be greater than 5' }
Explanation: NaN is not greater than any number.
Constraints
- The matcher must be implemented using TypeScript.
- The matcher should be compatible with Jest's assertion API.
- The matcher should not introduce any external dependencies.
- The matcher should be reasonably performant; avoid unnecessary computations.
Notes
- You'll need to define a Jest custom matcher factory function. This function will receive the received value and the argument as parameters.
- The matcher factory function should return a function that takes the argument and returns the result object with
passandmessageproperties. - Consider using template literals for constructing the message to improve readability.
- Remember that the
messageproperty should be a function that returns the message string. This allows Jest to dynamically include the received and expected values in the message.