Skip to content
← The curriculum

The toBeTruthy trap

Why asserting on truthiness is barely an assertion at all, and what to write instead.

expect(result).toBeTruthy() is the single most common way a test manages to assert nothing. It passes for true, 1, "anything", [], {}, and every object your function could possibly return.

The set of values it rejects is tiny: false, 0, "", null, undefined and NaN. Everything else is fine by it. So a function that returned the wrong string, the wrong object, or a completely unrelated value would sail past.

It is popular because it is what you reach for when you do not yet know what the answer should be — and that is exactly the moment to stop and work out what the answer should be.

1 of 5