Jest provides various test matchers to verify different conditions:
Equality Matchers:
toBe(value): Strict equality (===)toEqual(value): Deep equalitytoStrictEqual(value): Strict deep equality (including undefined properties)toMatchObject(object): Partial object matching
Truthiness Matchers:
toBeNull(): Matches only nulltoBeUndefined(): Matches only undefinedtoBeDefined(): Not undefinedtoBeTruthy(): Truthy valuestoBeFalsy(): 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 or substring
Array Matchers:
toContain(item): Contains elementtoContainEqual(item): Contains equal elementtoHaveLength(number): Array lengthtoBeArray(): Is an array
Object Matchers:
toHaveProperty(keyPath, value): Has specific propertytoMatchObject(object): Partial object matching
Function Matchers:
toHaveBeenCalled(): Was calledtoHaveBeenCalledWith(...args): Called with specific argumentstoHaveBeenCalledTimes(n): Number of callstoHaveReturned(): Has return valuetoHaveReturnedWith(value): Returned specific value
Exception Matchers:
toThrow(error?): Throws errortoThrowErrorMatchingSnapshot(): Error snapshot
Custom Matchers:
javascriptexpect.extend({ toBeWithinRange(received, floor, ceiling) { const pass = received >= floor && received <= ceiling; return { pass, message: () => pass ? `expected ${received} not to be within range ${floor}-${ceiling}` : `expected ${received} to be within range ${floor}-${ceiling}` }; } }); test('number within range', () => { expect(100).toBeWithinRange(90, 110); });
Negation Matchers:
All matchers can be negated using .not:
javascriptexpect(value).not.toBe(42); expect(array).not.toContain('item');