Hone logo
Hone
Problems

Implementing the toBe Matcher in Jest

Jest's toBe matcher is a fundamental part of writing effective unit tests. It performs a strict equality check (using the === operator) between two values. This challenge asks you to implement a simplified version of the toBe matcher to solidify your understanding of how matchers work within Jest and how equality is evaluated in JavaScript/TypeScript.

Problem Description

Your task is to create a TypeScript function named toBe that mimics the core functionality of Jest's toBe matcher. This function should take two arguments, received and expected, and return a string indicating whether the test passed or failed based on a strict equality comparison (===). The returned string should follow the format: "Expected: <expected> to be <received>". If the values are strictly equal, the string should indicate a passing test. If they are not, it should indicate a failing test.

Key Requirements:

  • The function must be written in TypeScript.
  • It must accept two arguments: received (the value being tested) and expected (the expected value).
  • It must use the strict equality operator (===) to compare the received and expected values.
  • It must return a string that clearly indicates whether the test passed or failed, including both the expected and received values.
  • The returned string must adhere to the specified format: "Expected: <expected> to be <received>".

Expected Behavior:

  • If received === expected, the function should return "Expected: <expected> to be <received>".
  • If received !== expected, the function should return "Expected: <expected> to be <received>".

Edge Cases to Consider:

  • null and undefined values.
  • Different data types (e.g., string vs. number).
  • Objects and arrays (strict equality checks reference equality for objects and arrays).
  • NaN (NaN !== NaN, so consider how you want to handle this).

Examples

Example 1:

Input: toBe(5, 5)
Output: "Expected: 5 to be 5"
Explanation: The received value (5) is strictly equal to the expected value (5), so the test passes.

Example 2:

Input: toBe("hello", "world")
Output: "Expected: world to be hello"
Explanation: The received value ("hello") is not strictly equal to the expected value ("world"), so the test fails.

Example 3:

Input: toBe([1, 2], [1, 2])
Output: "Expected: [ 1, 2 ] to be [ 1, 2 ]"
Explanation: The received array ([1, 2]) is strictly equal to the expected array ([1, 2]), so the test passes. Note that this checks for reference equality.

Example 4:

Input: toBe(null, null)
Output: "Expected: null to be null"
Explanation: Handles null values correctly.

Constraints

  • The function must be named toBe.
  • The function must accept two arguments, received and expected, of type any.
  • The function must return a string.
  • The function should be concise and readable.
  • Performance is not a primary concern for this exercise.

Notes

  • This is a simplified implementation of toBe. Jest's actual matcher has more sophisticated features (e.g., handling different data types, providing more detailed error messages).
  • Focus on understanding the core concept of strict equality and how it's used in testing.
  • Consider how you want to handle NaN values. The standard === operator returns false when comparing NaN to anything, including itself. You could choose to treat NaN as equal to itself for the purpose of this exercise, but it's important to be aware of this behavior.
Loading editor...
typescript