AssertionError: Expected values to be equal
Error message
expect(received).toBe(expected) — Object.is equality
Quick summary
The most common test failure: your assertion expected one value but got another. The error message shows both values to help you diagnose the mismatch.
Why this happens
The function under test returned a different value than expected — the code has a bug.
The expected value in the test is wrong — the test itself has a mistake.
The test setup (Arrange phase) didn't prepare the right inputs or state.
A dependency returned unexpected data that propagated through the code.
Minimal example
test('add', () => {
expect(add(2, 3)).toBe(6); // wrong expectation
});test('add', () => {
expect(add(2, 3)).toBe(5); // correct expectation
});The expected value was wrong — 2 + 3 = 5, not 6. Fix the test expectation.
How to diagnose
Read the error message carefully — it shows 'Expected: X' and 'Received: Y'.
Check if the expected value in your test is correct by reviewing the requirements.
Manually trace through the code with the test inputs to see what the correct output should be.
If using mocks, verify the mock is returning the expected test data.
How to fix
If the code is wrong, fix the implementation to return the correct value.
If the test expectation is wrong, update the expected value in the assertion.
If the setup is wrong, fix the Arrange phase to provide correct inputs.
How to prevent
Use descriptive test names that document the expected behavior.
Start with simple cases and build up to complex ones.
Use TDD to clarify expected behavior before implementation.
Related Resources
Related Glossary
Related Lessons
- Assertions
Using assertions to verify behavior.