liveBook · Manning

Assertion

As explained in The Art of Unit Testing 3e

An assertion is a test condition that verifies whether an actual result or behavior satisfies an expected truth condition.

An assertion is the part of a test that checks whether actual behavior matches an expected condition. Without one, a test may run code but provide little evidence that anything is correct. A test that only verifies that code does not throw can be useful in rare cases, but its name should say so and the test should be explicit about that expectation. section 7.4.1

Write assertions around the behavior that matters, not every incidental detail. A large equality check for a returned collection can accidentally require its current order and object shape, making a test fail after harmless changes. Prefer smaller checks for relevant values; when order is irrelevant, locate the result for a particular input rather than relying on its array position. Match only the meaningful part of a string when exact wording is not the requirement. section 8.3.2

Keep assertions trustworthy and easy to diagnose:

  • Give each test one concern when possible. A failed assertion usually stops the test, so later checks do not run and cannot reveal additional failures. section 7.4.4
  • Use multiple assertions when they verify one coherent outcome, such as relevant properties of an object created by one operation. section 7.4.4
  • Separate actions from assertions, and use meaningful values rather than unexplained magic values so the expected behavior is readable. section 9.5
  • Do not add empty tests merely to increase code coverage; coverage alone does not demonstrate code quality. section 7.4.1

Check the two behaviors separately

A function can return a sum and call a callback. Those are separate concerns, so separate tests make failures clearer. One test can call trigger(1, 2, callback) and assert that the callback received "I'm triggered". Another can call trigger(1, 2, jest.fn()) and assert that the result is 3. If either behavior breaks, the failing test identifies it directly. section 7.4.4

Listing 8.16 Ignoring order and schema
test("ignore order and schema", () => {
  const pv5 = 
    new PasswordVerifier5([(input) => input.includes("abc")]);
 
  const results = pv5.verify(["a", "ab", "abc", "abcd"]);
 
  expect(results.length).toBe(4);
  expect(findResultFor("a")).toBe(false);
  expect(findResultFor("ab")).toBe(false);
  expect(findResultFor("abc")).toBe(true);
  expect(findResultFor("abcd")).toBe(true);
});

This example compares a fragile full-result expectation with checks that ignore unimportant ordering and schema details. section 8.3.2

How The Art of Unit Testing 3e covers it

How deep it goes
Explains assertion placement, failure behavior, multiple concerns, and overspecification without detailing framework internals.
Examples
Uses Jest expectations for no-throw behavior, callbacks and sums, person objects, password-verifier results, and string messages.
What it assumes
Introduces assertions through simple JavaScript tests without assuming prior framework knowledge.
What you can do after
Apply it to write focused, maintainable checks that expose the behavior that actually failed.
How it connects
Connects assertions to unit tests, code coverage, test naming, callbacks, collection ordering, and test setup.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.