Assertions
An assertion is a check that fails the test when a value is not what you claimed. In Jest and Vitest you write expect(value).matcher(expected).
Detailed Explanation
A test without an assertion is a smoke run. The assertion is the specification: expect(sum(2, 2)).toBe(4) says what "correct" means. When it fails, the runner prints a diff.
toBe is Object.is (think === plus NaN equals NaN). Use it for primitives and for "same object". toEqual is deep equality for plain data: arrays and objects compare by contents. toStrictEqual also checks undefined keys and prototypes. Mixing them is the usual review comment: toBe on an object is almost always wrong.
Other everyday matchers: toBeTruthy / toBeFalsy (prefer a precise toBe(true)), toBeNull, toBeUndefined, toContain, toHaveLength, toMatch (regex), toThrow / toThrowError (pass a function, not the thrown call), toHaveBeenCalledWith on mocks. Asymmetric matchers: expect.any(Function), expect.objectContaining({ id: 7 }).
Node's assert module (assert.equal, assert.deepStrictEqual) is the stdlib version. node:test uses assert. Do not mix == in assertions; assert.equal is loose, strictEqual is not.
Async assertions: await expect(promise).resolves.toBe(1) and .rejects.toThrow(...). A bare expect(promise).resolves without await is a false pass.
What it costs you: weak matchers. toBeTruthy accepts 1 and "ok". toEqual can hide extra fields you care about if you only check a subset — use objectContaining on purpose. Custom matchers belong in a expect.extend helper, not copied into every file.
Reach for the most specific matcher that still reads well. Reach for objectContaining when the payload has noise (timestamps). Reach for snapshots only when the tree is large and stable.
One assertion per idea is a guideline, not a law. A single toEqual on a result object is better than ten toBe lines that drift when you add a field. Conversely, a 200-line toEqual on a whole page props object hides the one field that matters. Assert the behaviour you would explain to a reviewer.
Custom error classes should be asserted with toBeInstanceOf plus a message matcher, so a refactor of the wording can be a considered change rather than a silent swap of error types.
Code Examples
const user = { id: 7, email: "ada@example.com" };
expect(user.id).toBe(7);
expect(user).toEqual({ id: 7, email: "ada@example.com" });
expect(user).toBe(user);
expect({ id: 7 }).not.toBe({ id: 7 });
expect(user).toEqual(expect.objectContaining({ id: 7 }));function boom() {
throw new Error("nope");
}
expect(boom).toThrow("nope");
expect(() => boom()).toThrow(/no/);await expect(Promise.resolve(1)).resolves.toBe(1);
await expect(Promise.reject(new Error("x"))).rejects.toThrow("x");Common Gotchas
`expect(fn()).toThrow` calls `fn` before the matcher
pass `() => fn()` or `fn` itself.
`expect(p).resolves` without `await` does not fail the test
always await.
`toBe` on objects tests identity
two equal literals fail. Use `toEqual`.
`toEqual` vs `toStrictEqual`
`{ a: undefined }` vs `{}` is equal for `toEqual` and not for `toStrictEqual`.