liveBook · Manning

Property-based testing

As explained in Functional Programming in JavaScript

A testing approach that checks whether a program satisfies general properties across many generated inputs rather than only fixed example cases.

Property-based testing checks a general statement about a function across many generated inputs instead of relying only on a few fixed examples. It works especially well for pure, referentially transparent functions because their results depend clearly on their inputs and have no surrounding side effects. section 6.4 section 6.0

You describe the required property, choose generators for valid inputs, and let a testing library exercise many randomized cases. Haskell’s QuickCheck provides the canonical model; JavaScript uses JSCheck for the same style of specification-driven testing. section 6.4

A practical property-based test usually combines these parts:

  • A property states what the function’s output must satisfy. section 6.4
  • A generator produces many inputs, such as arrays of numbers or structured identifiers. section 6.4
  • A verdict function converts each result into a pass or fail decision, while reports show the generated cases. section 6.4

Testing grade calculations

A JSCheck test can assert that generated arrays containing numbers from 90 to 100 always produce an A grade. The framework reports 100 generated cases and their pass or fail results. A custom generator can similarly create formatted Social Security numbers and test a length-checking function across many inputs. section 6.4

Listing 6.4. Property-based test for computeAverageGrade
JSC.clear();                                                              #1
JSC.on_report((str) => console.log(str));
JSC.test(
   'Compute Average Grade',                                               #2
   function (verdict, grades, grade) {                                    #3
      return verdict(computeAverageGrade(grades) === grade);
   },
   [
     JSC.array(JSC.integer(20), JSC.number(90,100)),                      #4
     'A'
   ],
   function (grades, grade) {                                             #5
     return 'Testing for an ' + grade + ' on grades: ' + grades;
   }         
);

The listing shows the core JSCheck shape: clear the runner, define a verdict function, generate input arrays, and classify each test. section 6.4

How Functional Programming in JavaScript covers it

How deep it goes
Explains properties, generators, verdicts, reports, custom specifiers, and QUnit integration through JavaScript examples.
Examples
Demonstrates grade classification, generated Social Security numbers, pass reports, invalid properties, and QUnit integration.
What it assumes
Assumes familiarity with unit tests while building on pure functions, clear inputs, and referentially transparent specifications.
What you can do after
Apply it via JSCheck to generate randomized inputs, evaluate function properties, and inspect pass or failure reports.
How it connects
Connects pure functions, referential transparency, unit testing, QuickCheck, JSCheck, and QUnit.

Also explained in

Compare how 8 books explain property-based testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.