What are the most commonly used Jest matchers?

Intermediate

Answer

Jest provides many built-in matchers for different assertion types:

Equality matchers:

  • toBe(): Exact equality (Object.is)
  • toEqual(): Deep equality
  • toStrictEqual(): Strict deep equality

Truthiness matchers:

  • toBeTruthy(): Truthy values
  • toBeFalsy(): Falsy values
  • toBeNull(): Specifically null
  • toBeUndefined(): Specifically undefined

Number matchers:

  • toBeGreaterThan(): Greater than
  • toBeCloseTo(): Floating point numbers

String matchers:

  • toMatch(): Regular expressions
  • toContain(): Substring matching

Array/Object matchers:

  • toContain(): Array contains item
  • toHaveProperty(): Object has property