What Is an Assertion?
An assertion is the statement in a test that checks an actual result against an expected one, deciding whether the test passes or fails.
Every test ultimately comes down to assertions. An assertion declares "this should be true," and if it is not, the test fails. The quality of a test lives in its assertions: a test that runs a lot of code but asserts little proves almost nothing. Sharp assertions are what give a green check meaning.
What an assertion does
An assertion compares an actual value to an expectation and raises a failure if they do not match. The failure carries a message explaining what was expected versus what occurred, which is what makes a failing test actionable rather than just red.
Common kinds of assertions
- Equality: actual equals expected.
- Truthiness: a value is true, present, or non-null.
- Exceptions: a call throws the expected error.
- Collection and shape checks: contains, length, matches.
A quick example
An assertion states the expected outcome; a clear failure message points straight at the discrepancy.
expect(total).toBe(42);
// on failure: expected 42, received 40What makes a good assertion
Good assertions are specific and behavior-focused: they check the outcome that matters, not incidental implementation details. Too few assertions leave bugs uncaught (the problem mutation testing exposes); over-specific ones make tests brittle. Aim for assertions that fail when the behavior is wrong and only then.
Assertions and CI signal
In CI, the assertion is what determines pass or fail, and its message is what the developer reads on a broken build. Strong assertions plus a fast, reliable runner mean a red build is both meaningful and quick to diagnose, which is the whole point of automated testing.
Key takeaways
- An assertion checks an actual result against an expectation.
- Test quality lives in the sharpness of its assertions.
- Good assertions fail exactly when the behavior is wrong.