Skip to content

Assertion without message: unclear failure

Quick summary

Your assertion failed but the error message is generic — it is unclear what was expected and what was received.

Why this happens

  • The assertion has no custom message, so the framework shows a generic diff

  • The assertion is complex and the default message does not explain the intent

  • Multiple assertions in one test make it unclear which one failed

Minimal example

✗ Broken code
test('user profile', () => {
  const user = getUser(1);
  expect(user.name).toBe('Alice'); // unclear if this fails
  expect(user.age).toBe(30); // or this fails
  expect(user.email).toContain('@'); // or this
});
✓ Fixed code
test('user name is correct', () => {
  expect(getUser(1).name).toBe('Alice');
});
test('user age is correct', () => {
  expect(getUser(1).age).toBe(30);
});
test('user email is valid', () => {
  expect(getUser(1).email).toContain('@');
});

Splitting assertions into separate tests with descriptive names makes it clear exactly which assertion failed and why.

How to diagnose

  • Review the test failure output to see if the message explains what was expected

  • Check if multiple assertions in one test make it hard to identify the failing one

  • Add descriptive messages to assertions to clarify intent

How to fix

  • Add a custom message to the assertion: expect(value).toBe(expected, 'user age should match')

  • Split multiple assertions into separate test cases with descriptive names

  • Use more specific matchers like toContain or toMatchObject for better error output

How to prevent

  • Always add descriptive messages to assertions for non-obvious checks

  • Keep one logical assertion per test and use descriptive test names

Related Resources

Related Glossary

Related Lessons

← Back to language