1.0.1 • Published 6 years ago

test-value-generator v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

test-value-generator

Utilities to generate incremental and unique values in automated tests.

npm version Dependency Status Build Status Coverage Status License

Developed at the Media Engineering Institute (HEIG-VD).

Usage

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

Generating incremental values

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"

Generating unique values

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.
}