Hone logo
Hone
Problems

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 expected value.
  • It should compare the received value (provided by Jest) with the expected value.
  • 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 received is a number and received < expected, the test passes.
  • If received is a number and received >= expected, the test fails with a message like "Expected <received> to be less than <expected>".
  • If received is not a number or expected is not a number, the test should fail with an informative error message.

Edge Cases to Consider:

  • Comparing with Infinity and -Infinity.
  • Comparing with NaN.
  • Cases where received or expected are 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 expected value passed to toBeLessThan can be any JavaScript value, but the matcher will specifically check if it's a finite number for comparison.
  • The received value (from expect()) 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 Matchers interface to include your custom matcher for proper TypeScript typing.
  • Consider using Jest's this.utils for messages and other utilities.
  • Think about how to handle NaN, Infinity, and -Infinity in your comparisons.
  • The core logic will involve checking the types of received and expected before performing the numerical comparison.
Loading editor...
typescript