Matchers¶
Complete reference for all available matchers in rjest.
Basic Matchers¶
toBe(value)¶
Strict equality using Object.is:
expect(2 + 2).toBe(4);
expect('hello').toBe('hello');
expect(true).toBe(true);
// Note: Objects compare by reference
const obj = { a: 1 };
expect(obj).toBe(obj); // passes
expect({ a: 1 }).toBe({ a: 1 }); // fails!
toEqual(value)¶
Deep equality for objects and arrays:
expect({ a: 1, b: 2 }).toEqual({ a: 1, b: 2 });
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect({ a: { b: { c: 1 } } }).toEqual({ a: { b: { c: 1 } } });
toStrictEqual(value)¶
Strict deep equality (checks undefined properties):
expect({ a: 1 }).toStrictEqual({ a: 1 });
expect({ a: undefined }).not.toStrictEqual({}); // different!
Truthiness¶
toBeTruthy()¶
Value is truthy:
expect(1).toBeTruthy();
expect('hello').toBeTruthy();
expect([]).toBeTruthy();
expect({}).toBeTruthy();
toBeFalsy()¶
Value is falsy:
expect(0).toBeFalsy();
expect('').toBeFalsy();
expect(null).toBeFalsy();
expect(undefined).toBeFalsy();
expect(NaN).toBeFalsy();
toBeNull()¶
Value is null:
toBeUndefined()¶
Value is undefined:
toBeDefined()¶
Value is not undefined:
toBeNaN()¶
Value is NaN:
Numbers¶
toBeGreaterThan(number)¶
toBeGreaterThanOrEqual(number)¶
toBeLessThan(number)¶
toBeLessThanOrEqual(number)¶
toBeCloseTo(number, precision?)¶
Floating point comparison:
Strings¶
toMatch(regexp | string)¶
String matches pattern:
expect('hello world').toMatch('world');
expect('hello world').toMatch(/world/);
expect('[email protected]').toMatch(/^[\w.]+@[\w.]+$/);
toHaveLength(number)¶
String (or array) has length:
Arrays¶
toContain(item)¶
Array contains item (uses ===):
toContainEqual(item)¶
Array contains item with deep equality:
toHaveLength(number)¶
Array has length:
Objects¶
toHaveProperty(path, value?)¶
Object has property:
// Check property exists
expect({ a: 1 }).toHaveProperty('a');
// Check nested property
expect({ a: { b: 1 } }).toHaveProperty('a.b');
// Check property value
expect({ a: 1 }).toHaveProperty('a', 1);
// Check array index
expect({ items: [1, 2, 3] }).toHaveProperty('items.1', 2);
toBeInstanceOf(Class)¶
Value is instance of class:
expect(new Date()).toBeInstanceOf(Date);
expect([]).toBeInstanceOf(Array);
expect(new Error()).toBeInstanceOf(Error);
toMatchObject(object)¶
Object contains subset:
Errors¶
toThrow(error?)¶
Function throws:
// Any error
expect(() => { throw new Error(); }).toThrow();
// Error message contains string
expect(() => { throw new Error('Invalid input'); }).toThrow('Invalid');
// Error message matches regex
expect(() => { throw new Error('Error code: 404'); }).toThrow(/\d+/);
// Error is instance of class
expect(() => { throw new TypeError(); }).toThrow(TypeError);
Mock Functions¶
toHaveBeenCalled()¶
Mock was called:
toHaveBeenCalledTimes(number)¶
Mock was called N times:
toHaveBeenCalledWith(arg1, arg2, ...)¶
Mock was called with arguments:
toHaveBeenLastCalledWith(arg1, arg2, ...)¶
Last call had arguments:
const mock = jest.fn();
mock('first');
mock('second');
expect(mock).toHaveBeenLastCalledWith('second');
toHaveReturned()¶
Mock returned (didn't throw):
toHaveReturnedWith(value)¶
Mock returned specific value:
Async Matchers¶
resolves¶
Promise resolves:
await expect(Promise.resolve(42)).resolves.toBe(42);
await expect(fetchData()).resolves.toEqual({ id: 1 });
rejects¶
Promise rejects:
await expect(Promise.reject(new Error('fail'))).rejects.toThrow('fail');
await expect(failingOperation()).rejects.toBeInstanceOf(Error);
Negation¶
.not¶
Invert any matcher:
expect(1).not.toBe(2);
expect([1, 2]).not.toContain(3);
expect({ a: 1 }).not.toHaveProperty('b');
expect(() => {}).not.toThrow();
Snapshot Matchers¶
toMatchSnapshot(hint?)¶
Match against stored snapshot:
expect({ user: 'Alice', role: 'admin' }).toMatchSnapshot();
expect(render(<Component />)).toMatchSnapshot('component render');
toMatchInlineSnapshot(snapshot?)¶
Match against inline snapshot:
Common Patterns¶
Testing Objects¶
const user = { id: 1, name: 'Alice', email: '[email protected]' };
expect(user).toEqual({
id: expect.any(Number),
name: 'Alice',
email: expect.stringMatching(/@/),
});
Testing Arrays¶
const items = [1, 2, 3, 4, 5];
expect(items).toHaveLength(5);
expect(items).toContain(3);
expect(items[0]).toBe(1);
expect(items).toEqual(expect.arrayContaining([1, 3, 5]));
Testing Errors¶
function validateEmail(email: string) {
if (!email.includes('@')) {
throw new Error('Invalid email format');
}
}
expect(() => validateEmail('invalid')).toThrow('Invalid email');
expect(() => validateEmail('[email protected]')).not.toThrow();