when fuzzed by

  • <any> [when] fuzzed by <function> <assertion>

A shorthand for the "to be valid for all" assertion for simple cases where you have a test subject and a function that returns a generator that "fuzzes", or somehow creates test cases, based on the subject.

const { integer } = require('chance-generators');
 
function makePrefixGenerator(str) {
  return integer({ min: 1, max: str.length - 1 }).map((prefixLength) =>
    str.substr(0, prefixLength)
  );
}
 
expect('abc', 'when fuzzed by', makePrefixGenerator, 'to match', /^a/);

The above succeeds because the generated prefixes are at least one character long, so every generated string will start with 'a'. If we change the minimum prefix length to 0, we will see it generate the empty string fail:

function makePrefixGenerator(str) {
  return integer({ min: 0, max: str.length - 1 }).map((prefixLength) =>
    str.substr(0, prefixLength)
  );
}
 
expect('abc', 'when fuzzed by', makePrefixGenerator, 'to match', /^a/);
Found an error after 4 iterations
counterexample:
 
  
Generated input: ''
with: fuzz({
  
value'abc',
mutatorinteger({ min0max2 }).map(function (prefixLength) {
  return str.substr(0, prefixLength);
})
})
 
expected '' to match /^a/