Implement toBeNull Matcher 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 such matcher is toBeNull, which checks if a value is strictly null. This challenge asks you to implement a custom Jest matcher that mimics the behavior of toBeNull. Understanding how these matchers are built will deepen your knowledge of Jest's internal workings and enhance your ability to write effective tests.
Problem Description
Your task is to create a custom Jest matcher named toBeNull. This matcher should be able to be used within a Jest test suite to assert that a given value is strictly null.
Key Requirements:
- The custom matcher should be named
toBeNull. - When called on a value, it should return
trueif the value is strictlynull. - When called on a value that is not strictly
null, it should returnfalse. - The matcher should integrate with Jest's custom matcher API.
Expected Behavior:
When you use expect(value).toBeNull(), the test should pass if value is null, and fail otherwise.
Edge Cases:
- Consider what happens when the input is
undefined. - Consider what happens when the input is
0,false,''(empty string), or an empty object/array.
Examples
Example 1:
// In a Jest test file (e.g., myMatcher.test.ts)
describe('myCustomMatchers', () => {
// Assuming your custom matcher is registered globally or imported
test('should pass when the value is null', () => {
expect(null).toBeNull();
});
});
Output (in Jest test runner): The test will pass.
Explanation:
The input null is strictly equal to null, so the toBeNull matcher correctly asserts this.
Example 2:
// In a Jest test file (e.g., myMatcher.test.ts)
describe('myCustomMatchers', () => {
test('should fail when the value is undefined', () => {
expect(undefined).toBeNull();
});
});
Output (in Jest test runner):
The test will fail with an error message similar to:
Expected "undefined" to be null.
Explanation:
The input undefined is not strictly equal to null, so the toBeNull matcher correctly asserts this failure.
Example 3:
// In a Jest test file (e.g., myMatcher.test.ts)
describe('myCustomMatchers', () => {
test('should fail when the value is a string', () => {
expect("hello").toBeNull();
});
test('should fail when the value is a number', () => {
expect(0).toBeNull();
});
test('should fail when the value is a boolean', () => {
expect(false).toBeNull();
});
test('should fail when the value is an object', () => {
expect({}).toBeNull();
});
});
Output (in Jest test runner):
All tests will fail with messages indicating the actual value received. For instance, for "hello":
Expected "hello" to be null.
Explanation:
None of the provided values ("hello", 0, false, {}) are strictly null, so the toBeNull matcher correctly identifies these as failures.
Constraints
- Your implementation must use TypeScript.
- You should leverage Jest's custom matcher API. A common approach is to extend
jest.Expect. - The matcher should perform a strict equality check (
===) fornull.
Notes
To create a custom Jest matcher, you'll typically extend the jest.Expect interface and implement your matcher logic. You can then use expect.extend() to register your custom matchers.
Consider how your matcher will provide informative error messages when the assertion fails. Jest's custom matcher API expects you to return an object with pass (a boolean indicating success or failure) and message (a function that generates the error message).