Skip to content

Jest expect.objectContaining Not Matching Fix

DodaTech Updated 2026-06-24 2 min read

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 in toEqual
  • 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

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What's the difference between toMatchObject and expect.objectContaining?

toMatchObject() is a standalone matcher that checks if an object contains a subset of properties. expect.objectContaining() is an asymmetric matcher that can be nested inside other matchers like toEqual(). Both do similar things — use toMatchObject for simple cases and objectContaining for nesting.

Can I use objectContaining to check nested objects?

Yes. Nest expect.objectContaining() calls: expect(user).toEqual(expect.objectContaining({ address: expect.objectContaining({ city: 'New York' }) })).

How do I check that an object has a specific key regardless of value?

Use expect(object).toHaveProperty('key'). This checks existence only. For type checking, use expect(object).toHaveProperty('key', expect.any(Number)).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro