Hone logo
Hone
Problems

Implementing toBeNull in Jest

Jest, a popular JavaScript testing framework, provides a rich set of matchers for asserting values. However, Jest doesn't natively include a toBeNull matcher. This challenge asks you to implement a custom Jest matcher that accurately checks if a value is strictly equal to null. This is useful for ensuring that variables or function return values are explicitly set to null when expected, preventing unexpected behavior due to undefined or other values.

Problem Description

You need to create a custom Jest matcher named toBeNull. This matcher should extend Jest's expect.matcher functionality and provide a method that compares the received value with null using strict equality (===). The matcher should return true if the value is strictly equal to null, and false otherwise. The matcher should also provide a descriptive failure message when the assertion fails, clearly indicating that the value was not null.

Key Requirements:

  • Extend expect.matcher: Your matcher must be properly registered with Jest's expect object.
  • Strict Equality: Use the strict equality operator (===) for comparison.
  • Descriptive Failure Message: Provide a clear and informative message when the assertion fails. The message should indicate that the value was not null and display the actual value.
  • Correct Return Value: The matcher should return a boolean value indicating whether the assertion passed or failed.

Expected Behavior:

  • expect(null).toBeNull() should pass.
  • expect(undefined).toBeNull() should fail with a descriptive message.
  • expect(0).toBeNull() should fail with a descriptive message.
  • expect("null").toBeNull() should fail with a descriptive message.
  • expect(nullishValue).toBeNull() should pass, where nullishValue is a variable assigned to null.

Edge Cases to Consider:

  • The input value can be of any type.
  • The matcher should handle cases where the input is not a primitive value (e.g., an object or array).

Examples

Example 1:

Input: expect(null).toBeNull()
Output: Assertion passes
Explanation: The value is strictly equal to null.

Example 2:

Input: expect(undefined).toBeNull()
Output: Assertion fails with message: "Expected undefined to be null"
Explanation: The value is undefined, not null.

Example 3:

Input: expect({}).toBeNull()
Output: Assertion fails with message: "Expected {} to be null"
Explanation: The value is an object, not null.

Constraints

  • The implementation must be in TypeScript.
  • The matcher must be compatible with Jest's existing matcher infrastructure.
  • The matcher should be performant; avoid unnecessary computations.
  • The matcher should not introduce any dependencies beyond Jest itself.

Notes

  • You'll need to use Jest's expect.matcher API to define your custom matcher. Refer to the Jest documentation for details on how to extend matchers.
  • Consider how to format the failure message to be both informative and user-friendly.
  • Think about how to handle different data types gracefully. Strict equality (===) is key here.
  • The matcher should be designed to be reusable and easy to understand.
Loading editor...
typescript