Jest provides rich assertion methods, primarily used through the expect() function combined with matchers:
Common Matchers:
Equality Matchers:
toBe(value): Strict equality (using===)toEqual(value): Deep equality (recursively compares objects and arrays)toMatchObject(object): Partial object property matchingtoStrictEqual(value): Strict deep equality (including undefined properties)
Truthiness Matchers:
toBeNull(): Matches only nulltoBeUndefined(): Matches only undefinedtoBeDefined(): Not undefinedtoBeTruthy(): Truthy values (not false, 0, "", null, undefined, NaN)toBeFalsy(): Falsy values
Number Matchers:
toBeGreaterThan(number): Greater thantoBeGreaterThanOrEqual(number): Greater than or equaltoBeLessThan(number): Less thantoBeLessThanOrEqual(number): Less than or equaltoBeCloseTo(number, precision): Floating point approximate equality
String Matchers:
toMatch(regexp | string): Matches regex or stringtoContain(item): Contains element
Async Matchers:
resolves: Promise resolvesrejects: Promise rejects
Examples:
javascriptexpect(2 + 2).toBe(4); expect({ name: 'John' }).toEqual({ name: 'John' }); expect(null).toBeNull(); expect(10).toBeGreaterThan(5); expect('hello').toMatch(/ell/);