toMatchObject Partial Match Failure
Error message
Expected object to match — property value mismatch
Quick summary
toMatchObject checks that the received object contains at least the expected properties with matching values. A property value didn't match.
Why this happens
A property in the received object has a different value than expected.
The expected object has a property that doesn't exist in the received object.
Nested object properties don't match.
Type mismatch — string vs number for the same property.
Minimal example
test('user', () => {
expect({ id: 1, name: 'Alice', age: 30 })
.toMatchObject({ name: 'Bob' }); // fails — name is Alice
});test('user', () => {
expect({ id: 1, name: 'Alice', age: 30 })
.toMatchObject({ name: 'Alice' }); // passes
});Fix the expected value to match the actual property value in the received object.
How to diagnose
Read the diff in the error output to see which property doesn't match.
Check if the expected object has properties not present in the received object.
Verify nested object structure matches.
Check for type mismatches (e.g., '5' vs 5).
How to fix
Fix the expected values to match the actual output.
Fix the code to produce the correct property values.
Remove properties from the expected object that shouldn't be checked.
For exact matching, use toEqual instead of toMatchObject.
How to prevent
Understand the difference: toMatchObject (partial), toEqual (exact), toStrictEqual (exact + type).
Use the right matcher for your intent.
Related Resources
Related Glossary
Related Lessons
- Jest Basics
Getting started with Jest.