Back to Projects
Case Study

Jest Wise

A simple add() function tested with Jest using white-box techniques. Found 3 bugs that black-box testing would never catch — string concatenation, NaN on undefined, and silent null handling. Fixed the function. Proved it with 7 passing tests.

At a Glance

Core Technologies

Unit TestingWhite-box TestingJestJavaScript
Jest Wise

The problem

Black-box testing checks inputs and outputs. White-box testing checks what happens inside. This project was built to show the difference — using one simple function that looks correct but breaks in three ways.

The function

add(a, b) returns a + b. Four inputs work fine. Three inputs silently produce wrong results. No errors thrown, no warnings — just wrong output.

What black-box testing misses

  • add("2", 3) returns "23" — string concatenation instead of addition.
  • add(undefined, 3) returns NaN — no error, just a useless result.
  • add(null, 1) returns 1 — silently wrong, hardest to catch.

None of these throw errors. A black-box tester sees output and moves on. A white-box tester reads the code and asks: what happens if the type is wrong?

The fix

One type check at the top of the function:

if (typeof a !== "number" || typeof b !== "number") {
throw new Error("Both inputs must be numbers");
}

Now the function fails loudly instead of silently. Loud failures are easier to debug than silent wrong answers.

Test results

7 test cases written. 4 passed before the fix. 7 passed after.

  • Valid integers — Pass both
  • Zero addition — Pass both
  • Negative numbers — Pass both
  • Big number input — Pass both
  • String input — Fail before, Pass after
  • Undefined input — Fail before, Pass after
  • Null input — Fail before, Pass after

What I learned

Silent failures are more dangerous than loud ones. The original function never crashed — it just returned wrong values.

In a real application, add("2", 3) returning "23" could corrupt a cart total, a salary calculation, or a test score. No error log. No alert. Just wrong data.

Type validation is the first line of defense.

Tools

  • Test framework: Jest
  • Report: @mallikgalibshahriar/failsafe-report
  • Language: JavaScript
  • Concepts: White-box testing, unit testing, boundary value analysis, type validation