Jest expect.objectContaining Not Matching Fix
In this tutorial, you'll learn about Jest expect.objectContaining Not Matching Fix. We cover key concepts, practical examples, and best practices.
Your expect.objectContaining() assertion fails because the actual object has extra properties or the types don't match exactly.
The Problem
// WRONG — exact match when object has extra properties
const user = { id: 1, name: 'Alice', email: 'alice@example.com', role: 'admin' };
expect(user).toEqual({ id: 1, name: 'Alice' }); // FAILS — extra properties
Expected: {"id": 1, "name": "Alice"}
Received: {"id": 1, "name": "Alice", "email": "alice@example.com", "role": "admin"}
toEqual() requires an exact match. Any extra properties in the actual object cause the assertion to fail.
Step-by-Step Fix
1. Use objectContaining for partial matches
// RIGHT — match only specific properties
expect(user).toEqual(
expect.objectContaining({
id: 1,
name: 'Alice',
})
);
// Passes — ignores extra properties
2. Combine with other asymmetric matchers
// RIGHT — flexible property matching
const order = {
id: 'ord_123',
createdAt: new Date('2024-06-15'),
total: 99.99,
items: ['book', 'pen'],
};
expect(order).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^ord_/),
createdAt: expect.any(Date),
total: expect.any(Number),
items: expect.arrayContaining(['book']),
})
);
3. Use toMatchObject for partial deep matching
// RIGHT — toMatchObject checks subset
expect(user).toMatchObject({ id: 1, name: 'Alice' });
expect(user).toMatchObject({ role: 'admin' });
// Works with nested objects too
const profile = {
user: { id: 1, name: 'Alice' },
settings: { theme: 'dark' },
};
expect(profile).toMatchObject({
user: { name: 'Alice' },
});
4. Check property existence without value
// RIGHT — property existence
expect(user).toHaveProperty('id');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('email');
expect(user).not.toHaveProperty('ssn');
Expected output:
PASS tests/object.test.js
✓ matches partial object (4 ms)
✓ combines matchers (6 ms)
✓ checks property existence (3 ms)
Prevention Tips
- Use
expect.objectContaining()for partial matches intoEqual - Use
toMatchObject()for simpler partial matching - Use
toHaveProperty()for property existence checks - Combine with
expect.any(),expect.stringMatching(), etc. - Use
expect.not.objectContaining()for negative assertions
Common Mistakes with expect object match
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro