Utilities to generate incremental and unique values in automated tests.
Developed at the Media Engineering Institute (HEIG-VD).
This module exposes factory functions that return value generator functions when called:
const testValueGenerator = require('test-value-generator');
// Create a value generator function:
const numberGenerator = testValueGenerator.incremental(i => i);
typeof(numberGenerator); // => "function"
// Generate a value by calling it:
numberGenerator(); // => 0
const testValueGenerator = require('test-value-generator');
// An incremental value generator helps you produce values containing
// a number that is incremented each time you use the generator.
const incrementalEmail = testValueGenerator.incremental(i => `email-${i}@example.com`);
incrementalEmail(); // => "email-0@example.com"
incrementalEmail(); // => "email-1@example.com"
incrementalEmail(); // => "email-2@example.com"
A value generator created with unique
will call your function until it can
generate a value that it has not generated before. It will throw an error if it
fails after 10 attempts.
const testValueGenerator = require('test-value-generator');
// A unique value generator makes sure to never produces the same value
// twice, and throws an error if it cannot.
const uniqueDiceRoll = testValueGenerator.unique(() => Math.floor(Math.random() * 6 + 1));
uniqueDiceRoll(); // => 3
uniqueDiceRoll(); // => 5
uniqueDiceRoll(); // => 6
uniqueDiceRoll(); // => 2
uniqueDiceRoll(); // => 1
uniqueDiceRoll(); // => 4
try {
uniqueDiceRoll();
} catch(err) {
// Error thrown: Could not generate unique value after 10 attempts.
}