Jest expect.arrayContaining Not Matching Fix
In this tutorial, you'll learn about Jest expect.arrayContaining Not Matching Fix. We cover key concepts, practical examples, and best practices.
Your expect.arrayContaining() assertion fails even though the array clearly contains the expected elements — or it passes when it shouldn't.
The Problem
// WRONG — using arrayContaining in the wrong position
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
];
expect(users).toEqual(
expect.arrayContaining({ id: 1, name: 'Alice' }) // FAILS — plain object, not matcher
);
Expected: {"id": 1, "name": "Alice"}
Received: [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
expect.arrayContaining() expects an array of matchers, not a single object.
Step-by-Step Fix
1. Use arrayContaining correctly
// RIGHT — wrap in array
expect(users).toEqual(
expect.arrayContaining([{ id: 1, name: 'Alice' }])
);
// Also works with partial matches
expect([1, 2, 3, 4]).toEqual(expect.arrayContaining([2, 4]));
expect(['apple', 'banana', 'cherry']).toEqual(expect.arrayContaining(['banana']));
2. Combine with other matchers
// RIGHT — nested matchers inside arrayContaining
expect([
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' },
]).toEqual(expect.arrayContaining([
expect.objectContaining({ name: 'Alice', role: 'admin' }),
]));
3. Use partial match with objects
// RIGHT — partial object match in array
const orders = [
{ orderId: 101, total: 50, status: 'shipped' },
{ orderId: 102, total: 75, status: 'pending' },
];
expect(orders).toEqual(
expect.arrayContaining([
expect.objectContaining({ status: 'shipped' }),
])
);
4. Check array length combined with contents
// RIGHT — length and content verification
const items = ['a', 'b', 'c'];
expect(items).toHaveLength(3);
expect(items).toEqual(expect.arrayContaining(['a', 'b', 'c']));
Expected output:
PASS tests/array.test.js
✓ matches array contents (5 ms)
✓ combines with matchers (4 ms)
✓ partial object match (6 ms)
Prevention Tips
- Always pass an array to
expect.arrayContaining() - Combine with
expect.objectContaining()for object arrays - Use
toHaveLength()alongside for size verification - Use
expect.not.arrayContaining()for negative assertions - Use
toContain()for single primitive values
Common Mistakes with expect array containing
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
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