Assertion
In one line
An assertion is the 'check' part of a test — it verifies that the actual result matches the expected result.
In simple words
An assertion is the heart of a test. It's a statement that says 'I expect this to be true.' If it's true, the test passes. If it's false, the test fails.
For example, expect(result).toBe(5) is an assertion. It checks that result equals 5. If result is 5, the assertion passes. If result is anything else, the assertion fails and the test is marked as failed.
Different types of assertions check different things: equality (toBe), deep equality (toEqual), truthiness (toBeTruthy), array membership (toContain), whether a function throws (toThrow), and many more.
Example
expect(2 + 2).toBe(4); // equality
expect([1, 2]).toContain(2); // membership
expect(null).toBeNull(); // null check
expect(() => throwErr()).toThrow(); // throwsEach line is a different type of assertion checking a different condition.
Related terms
Related Resources
Related Lessons
- Assertions
Using assertions to verify behavior.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.