Guide

To get the most out of the guide, you should have a basic understanding of nodejs, used some test suites before, and know some javascript.

The first thing to do is add jsverify as a devDependency to your project:

npm install -D jsverify

Test construction

You can create a test using checkForall which takes n args, with the last one being the predicate function the randomly generated arguments are passed to. The test is considered passing when the predicate returns true 100% of the time.

If all return true, the result of checkForAll is true

If any return false, the result of checkForAll is an Object containing additional data about what went wrong.

All tests created using checkForall will run 100 times by default. To change it, you have to use a different API which will be covered later.

For example:

checkForall(jsc.integer, (a) => … )

// A randomly generated integer will be passed to a predicate function as a. checkForall(jsc.integer, jsc.integer, (a, b) => … )

// Two randomly generated integers will be passed to a predicate function as a, b. checkForall(jsc.integer, jsc.integer, jsc.integer, (a, b, c) => … )

// Three randomly generated integers will be passed to a predicate function as a, b, c.

Working Example

Should you run the following code:

You will find the following output:

OK, passed 100 tests

OK, passed 100 tests

{ additionIsC ommutative : true,

multiplicationIsDistributive: true }

Let’s see what happens if we assert that subtraction is commutative by adding this function to our suite:

Running this gives us the following output:

OK, passed 100 tests

OK, passed 100 tests

Failed after 1 tests and 4 shrinks. rngState: 095d6bac8937ef2140; Counterexample: 0; 1; [ 0, 1 ]

{ additionIsCommutative: true,

multiplicationIsDistributive: true,

subtractionIs Commutative :

{ counterexample: [ 0, 1 ],

counterexamplestr: '0; 1',

shrinks: 4,

exc: false,

tests: 1,

rngState: '095d6bac8937ef2140' } }

Because subtraction is not commutative, the test fails! The failure gives us an object that tells us that it passed in 0, 1 as arguments, and it was the first test. exc is false because an exception was not thrown. Theoretically, you could assert that a given property test must pass x percent of the time. (Perhaps an async function takes less than 100 ms 90% of the time maybe?)