Implement toHaveLength Jest Matcher in TypeScript
Jest is a powerful JavaScript testing framework known for its expressive matchers. One common matcher is toHaveLength, used to assert the length of arrays, strings, and other iterable objects. This challenge asks you to implement a custom Jest matcher that replicates the functionality of toHaveLength.
Problem Description
You need to create a custom Jest matcher named toHaveLength. This matcher should be able to:
- Assert that a received value (e.g., an array, string, or arguments object) has a specific length.
- Report clearly when the received value does not have the expected length.
- Handle cases where the received value might not have a
lengthproperty or is not of a type that typically has a length.
Your implementation should follow the Jest custom matcher API conventions.
Examples
Example 1:
// In your Jest setup file (e.g., setupTests.ts)
import './customMatchers'; // Assuming your custom matcher is here
// In your test file
expect([1, 2, 3]).toHaveLength(3);
Output: The test passes.
Example 2:
// In your Jest setup file
import './customMatchers';
// In your test file
expect('hello').toHaveLength(5);
Output: The test passes.
Example 3:
// In your Jest setup file
import './customMatchers';
// In your test file
expect([1, 2]).toHaveLength(3);
Output:
The test fails with a message similar to:
Expected "1,2" to have length 3 but got 2.
Example 4:
// In your Jest setup file
import './customMatchers';
// In your test file
expect(null).toHaveLength(0);
Output:
The test fails with a message similar to:
Expected "null" to have length 0.
Constraints
- The implementation must be in TypeScript.
- The custom matcher should be registered with Jest using
expect.extend. - The matcher should correctly handle valid inputs (arrays, strings, arguments objects).
- The matcher should gracefully handle inputs that do not have a
lengthproperty or are not of expected types. - The error messages should be clear and informative, indicating both the expected and received lengths.
Notes
- You'll need to understand how to extend Jest's
expectobject. - Consider the different types of values that have a
lengthproperty and how to check for it. - Think about how to provide a helpful message when the assertion fails, including the actual length of the received value.
- The Jest documentation on custom matchers is an excellent resource.
- The
receivedargument in your matcher will be the value being tested (e.g.,[1, 2, 3]). - The
expectedargument will be the length you are asserting against (e.g.,3). - Your matcher should return an object with
pass(boolean) andmessage(function returning string) properties.