JavaScript & TypeScript / Quiz

Matcher Autopsy: toBe Is Not toEqual

Object.is, deep equality, strictness, and the floating-point trap. Pick the wrong matcher and your test lies in both directions.

Difficulty
Easy
Format
Quiz
Points
100
Estimate
9 min

// MISSION BRIEF

Your Mission

A test that asserts the wrong thing is worse than no test: it passes while the bug ships, or fails while the code is fine.

Jest's matcher library is precise about identity vs equality vs strict equality, and most JavaScript testers use the first matcher that autocompletes. This quiz drills the differences: toBe, toEqual, toStrictEqual, toContain, toThrow, and asymmetric matchers.

Speed bonus active: sharp matcher instincts earn extra points.

// FIRST CONTACT

Battle teaser

expect({ id: 7 }).toBe({ id: 7 }) fails even though the objects look identical. Why?

  1. AtoBe only works on strings and booleans
  2. BtoBe compares with Object.is (reference identity for objects); two separate object literals are never the same reference. Use toEqual for structural comparison
  3. CNumbers inside objects must be asserted with toBeCloseTo
  4. DThe object needs a custom equals() method for toBe to work
Answers, scoring, hints, and the full battle stay sealed.

// SKILL TAGS

javascriptjestmatchersunit-testing