Expect Function
Question
What does expect() do in Jest?
Hint
expect(value).matcher(expected) is the assertion pattern.
Explanation
The expect() function wraps a value and provides matcher methods to test it. expect(1 + 1).toBe(2) checks exact equality. Common matchers: toEqual (deep equality), toBeTruthy, toBeFalsy, toContain, toHaveLength, toThrow, toBeNull, toBeUndefined, toBeGreaterThan, toMatchObject, toHaveBeenCalledWith. expect can also test negation: .not.toBe(expected). expect is the core of Jest assertions. Each expect is a checkpoint — if it fails, the test fails. Multiple expects per test are allowed but one assertion per test is cleaner.
Related Resources
Related Lessons
- Assertions
Learn assertions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.