Implement toBeTruthy for Jest
Jest is a popular JavaScript testing framework that provides a rich set of matchers for asserting the expected outcomes of your code. One of these fundamental matchers is toBeTruthy, which checks if a value is "truthy". This challenge asks you to implement a custom Jest matcher that mimics the behavior of expect().toBeTruthy().
Problem Description
Your task is to create a custom Jest matcher named toBeTruthy. This matcher should evaluate whether the received value in an assertion is considered "truthy" in JavaScript. A truthy value is any value that is not falsy. The falsy values in JavaScript are: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN.
Key Requirements:
- Create a custom matcher function that can be added to Jest's
expect. - The matcher should accept one argument: the
receivedvalue. - The matcher should return an object with two properties:
pass: A boolean indicating whether the assertion passed (trueif the value is truthy,falseotherwise).message: A function that returns the error message to be displayed when the assertion fails. This message should clearly state what was expected and what was received.
Expected Behavior:
- If the
receivedvalue is truthy, thepassproperty should betrue. - If the
receivedvalue is falsy, thepassproperty should befalse. - The
messagefunction should provide informative output for both passing and failing scenarios (Jest often handles the "not" cases automatically, but it's good practice to consider them).
Examples
Example 1:
// In your Jest test file:
expect(true).toBeTruthy();
expect(1).toBeTruthy();
expect("hello").toBeTruthy();
expect({}).toBeTruthy();
expect([]).toBeTruthy();
Output (if all assertions pass): No output (tests pass silently).
Explanation: All the provided values (true, 1, "hello", {}, []) are truthy in JavaScript.
Example 2:
// In your Jest test file:
expect(false).not.toBeTruthy();
expect(0).not.toBeTruthy();
expect("").not.toBeTruthy();
expect(null).not.toBeTruthy();
expect(undefined).not.toBeTruthy();
expect(NaN).not.toBeTruthy();
Output (if all assertions pass): No output.
Explanation: All the provided values (false, 0, "", null, undefined, NaN) are falsy in JavaScript, so the not.toBeTruthy() assertion correctly passes.
Example 3: Failing Assertion
// In your Jest test file:
expect(0).toBeTruthy();
Expected Output (if this assertion fails):
Expected 0 to be truthy.
Explanation: The value 0 is falsy in JavaScript, so the toBeTruthy() matcher should fail, and the error message clearly indicates that 0 was expected to be truthy but wasn't.
Constraints
- The solution must be implemented in TypeScript.
- You will need to use Jest's custom matcher API.
- The
messagefunction should generate a human-readable error string.
Notes
- Familiarize yourself with Jest's custom matcher API, particularly how to extend
jest.Expect. - Consider how to handle the
thiscontext within your custom matcher. - The core logic of the matcher will involve checking if a value evaluates to
truein a boolean context. - You'll need to register your custom matcher with Jest.