toBe Matcher
All Jest topics∙ Jest
toBe Matcher focuses on primitive identity or exact reference equality. It uses `expect(received).toBe(expected)` using `Object.is` semantics to confirm the received primitive or reference being exactly equal.
Syntax
expect(received).toBe(expected)
📝 Jest Example
👁 Expected Result
💡 Run the test from isolated state and read the matcher diff when it fails.
Output
toBe Matcher: pASS — status is ready
Line-by-Line Explanation
| Line | Meaning |
|---|---|
test('status is ready', () => { | In toBe Matcher, line 2 declares a named Jest test. |
const status = 'ready'; | In toBe Matcher, line 3 implements setup, action, or verification for this example. |
expect(status).toBe('ready'); | In toBe Matcher, line 4 creates an expectation for the received value. |
}); | In toBe Matcher, line 5 implements setup, action, or verification for this example. |
Real-World Uses
- 1Use toBe Matcher to verify primitive identity or exact reference equality.
- 2toBe Matcher is valuable in unit-testing fundamentals when the test must prove the received primitive or reference being exactly equal.
- 3A useful failure record for toBe Matcher contains Jest’s received-versus-expected equality message.
Common Mistakes
- 1toBe Matcher commonly fails because of using `toBe` for separately created objects or arrays.
- 2Starting toBe Matcher without known primitive values or the same object reference makes the result nondeterministic.
- 3For toBe Matcher, executing code without asserting the received primitive or reference being exactly equal is incomplete.
- 4Using toBe Matcher to cover deep structural equality creates the wrong test boundary.
Best Practices
- 1Prepare known primitive values or the same object reference before running toBe Matcher.
- 2Implement toBe Matcher with `expect(received).toBe(expected)` using `Object.is` semantics.
- 3Make the central toBe Matcher assertion prove the received primitive or reference being exactly equal.
- 4Preserve Jest’s received-versus-expected equality message whenever toBe Matcher fails.
Core behavior
- 1toBe Matcher target: primitive identity or exact reference equality.
- 2toBe Matcher API: `expect(received).toBe(expected)` using `Object.is` semantics.
- 3toBe Matcher expected result: the received primitive or reference being exactly equal.
- 4toBe Matcher primary risk: using `toBe` for separately created objects or arrays.
Implementation steps
- 1Set up toBe Matcher with known primitive values or the same object reference.
- 2For toBe Matcher, invoke the behavior that produces primitive identity or exact reference equality.
- 3In toBe Matcher, apply `expect(received).toBe(expected)` using `Object.is` semantics to the observed result.
- 4Finish toBe Matcher by asserting the received primitive or reference being exactly equal.
Verification
- 1Run toBe Matcher once with input that should satisfy the received primitive or reference being exactly equal.
- 2Add a negative toBe Matcher case that must produce a readable failure.
- 3Repeat toBe Matcher from fresh state to reveal shared-data or ordering dependencies.
- 4Diagnose toBe Matcher through Jest’s received-versus-expected equality message.
Scope
- 1toBe Matcher covers primitive identity or exact reference equality.
- 2toBe Matcher does not directly prove deep structural equality.
- 3Mocks and fixtures used by toBe Matcher must continue to match its real dependency contracts.
- 4For evidence outside the toBe Matcher process boundary, prefer `toEqual()` or `toStrictEqual()`.
Summary
- toBe Matcher setup: known primitive values or the same object reference.
- toBe Matcher action: `expect(received).toBe(expected)` using `Object.is` semantics.
- toBe Matcher assertion: the received primitive or reference being exactly equal.
- toBe Matcher diagnostics: Jest’s received-versus-expected equality message.
- toBe Matcher boundary: choose `toEqual()` or `toStrictEqual()` for deep structural equality.
Interview Questions
Q1. What does toBe Matcher verify?
Answer: toBe Matcher verifies primitive identity or exact reference equality.
Q2. Which Jest API is central to toBe Matcher?
Answer: The central toBe Matcher API is `expect(received).toBe(expected)` using `Object.is` semantics.
Q3. What proves toBe Matcher passed?
Answer: A passing toBe Matcher test shows the received primitive or reference being exactly equal.
Q4. What makes toBe Matcher unreliable?
Answer: A common toBe Matcher cause is using `toBe` for separately created objects or arrays.
Q5. When should another test type replace toBe Matcher?
Answer: Replace toBe Matcher with `toEqual()` or `toStrictEqual()` for deep structural equality.
Quick Quiz
Which approach correctly implements toBe Matcher?