Hone logo
Hone
Problems

Jest Custom Matcher for Array Element Presence

This challenge asks you to create a custom Jest matcher that verifies if a specific element exists within an array. This is a common requirement in testing to ensure that certain data points are present in collections, improving the clarity and maintainability of your test suites.

Problem Description

You need to implement a custom Jest matcher named toHaveElement that checks if a given array contains a specific element. This matcher should integrate seamlessly with Jest's assertion syntax.

Key Requirements:

  • The matcher should accept two arguments: the array to be checked and the element to look for.
  • It should pass if the element is found anywhere within the array.
  • It should fail if the element is not found in the array.
  • The failure message should be informative, indicating which element was not found in which array.
  • The matcher should handle various data types within the array (numbers, strings, objects, etc.) and for the element being searched.
  • Consider cases where the array might be empty.

Expected Behavior:

When using expect(array).toHaveElement(element), the test should pass if element is present in array, and fail otherwise.

Edge Cases:

  • An empty array.
  • Searching for undefined or null within the array.
  • Arrays containing mixed data types.
  • Arrays containing duplicate elements.

Examples

Example 1:

// In your test file (e.g., __tests__/arrayMatchers.test.ts)
expect([1, 2, 3]).toHaveElement(2);

Output:

The assertion passes.

Explanation:

The number 2 is present in the array [1, 2, 3].

Example 2:

// In your test file (e.g., __tests__/arrayMatchers.test.ts)
expect(['apple', 'banana', 'cherry']).toHaveElement('grape');

Output:

The assertion fails with a message similar to: Expected "grape" to be present in ["apple", "banana", "cherry"]

Explanation:

The string 'grape' is not present in the array ['apple', 'banana', 'cherry'].

Example 3:

// In your test file (e.g., __tests__/arrayMatchers.test.ts)
const obj1 = { id: 1 };
const obj2 = { id: 2 };
const arr = [obj1, obj2];
expect(arr).toHaveElement(obj1);

Output:

The assertion passes.

Explanation:

The exact object obj1 is present in the array arr.

Example 4: (Edge Case)

// In your test file (e.g., __tests__/arrayMatchers.test.ts)
expect([]).toHaveElement(5);

Output:

The assertion fails with a message similar to: Expected 5 to be present in []

Explanation:

The array is empty, so 5 cannot be present.

Constraints

  • The custom matcher should be implemented using TypeScript.
  • The solution should rely on standard JavaScript array methods.
  • Avoid using external libraries for the core matching logic.
  • The custom matcher should be added to Jest's global expect object.

Notes

To create a custom Jest matcher, you'll typically use expect.extend(). You'll need to define a function that returns an object with pass (boolean) and message (function) properties. The message function should return a string that explains why the assertion failed. Pay close attention to how Jest handles comparisons for different data types, especially objects. For object comparisons, a strict equality check (===) will be sufficient for this challenge, meaning it looks for the exact same object instance.

Loading editor...
typescript