yahoo/sharkhorse

Name: sharkhorse

Owner: Yahoo Inc.

Description: Javascript test factories

Forked from: aaronabramov/sharkhorse

Created: 2017-04-19 21:50:23.0

Updated: 2017-07-25 22:01:31.0

Pushed: 2017-04-04 11:16:49.0

Homepage: https://www.npmjs.com/package/sharkhorse

Size: 501

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Sharkhorse

Build Status codecov.io

Javascript Test factories
Summary
Defining Factories

Factory definitions are plain javascript objects that hold generator objects.

essage_factory.js
rt {generators} from 'sharkhorse';

rt const Message = {
id: generators.sequence(),
subject: generators.lorem().words(2),
from: {
    name: generators.name().full(),
    email: generators.email()
}

Building objects from factories

To build an object from a factory definition use a create or 'createMany` function the function would iterate through object's nested properties and evaluate all generators to their values

rt {create, createMany} from 'sharkhorse';
rt {Message} from './message_factory';

te(Message) // => {id: 1, subject: 'Lorem ipsum', from: {name: 'Nickolas Conrad', email: 'random_0@example.com'}}
te(Message) // => {id: 2, subject: 'Lorem ipsum', from: {name: 'Seth Edwards', email: 'random_1@example.com'}}

createMany() will create an array of objects

teMany(Message, 3) // [{...}, {...}, {...}]
generators
sequence()

generates an incrementing or decrementing number every time it's evaluated

rators.sequence() // 1, 2, ..
rators.sequence().decrement() // 1, 0, -1, ...
rators.sequence().startFrom(100) // 100, 101, 102, ...
number()

generates a random number

rators.number() // 285
rators.number().min(500) // 24029
rators.number().max(2)
rators.number().min(0).max(2)
randomItem(list)
rators.randomItem([1, 2, 3]) // one of the numbers
name()

generates a random name

rators.name() // Seth Edwards
rators.name().full() // Seth Edwards
rators.name().first() // Seth
rators.name().last() // Edwards
email()

generates a random unique email every time it's evaluated

rators.email() // random_0@example.com, random_1@example.com
templateString()

Tagged template string generator. Any generator passed as a template string value will be evaluated when passing the generator into create function

MyStr = generators.templateString`test${generators.sequence()}`;
te(MyStr); // test1
te(MyStr); // test2
te(MyStr); // test3
lorem()

generates random text

rators.lorem() // Lorem ipsum dolor sit amet, per in mazim...
rators.lorem().word()
rators.lorem().words(n)
rators.lorem().paraghaph()
rators.lorem().paraghaphs(n)
date()
rators.date() // Date() object
rators.date().jsTimestamp // 1457241758397
rators.date().unixTimestamp // 1457241758
create(FactoryDefinition)

generates a new factory object from the passed argument

rate.create(MessageFactory) // {id: 1, subject: 'lorem', ...}
createMany(FactoryDefinition, n)

same as create() but generates an array of factories


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.