Home

Awesome

Rosie

Rosie the Riveter

Rosie is a factory for building JavaScript objects, mostly useful for setting up test data. It is inspired by factory_bot.

To use Rosie you first define a factory. The factory is defined in terms of attributes, sequences, options, callbacks, and can inherit from other factories. Once the factory is defined you use it to build objects.

Usage

There are two phases of use:

  1. Factory definition
  2. Object building

Factory Definition: Define your factory, giving it a name and optionally a constructor function (game in this example):

Factory.define('game')
  .sequence('id')
  .attr('is_over', false)
  .attr('created_at', () => new Date())
  .attr('random_seed', () => Math.random())

  // Default to two players. If players were given, fill in
  // whatever attributes might be missing.
  .attr('players', ['players'], (players) => {
    if (!players) {
      players = [{}, {}];
    }
    return players.map((data) => Factory.attributes('player', data));
  });

Factory.define('player')
  .sequence('id')
  .sequence('name', (i) => {
    return 'player' + i;
  })

  // Define `position` to depend on `id`.
  .attr('position', ['id'], (id) => {
    const positions = ['pitcher', '1st base', '2nd base', '3rd base'];
    return positions[id % positions.length];
  });

Factory.define('disabled-player').extend('player').attr('state', 'disabled');

Object Building: Build an object, passing in attributes that you want to override:

const game = Factory.build('game', { is_over: true });
// Built object (note scores are random):
//{
//    id:           1,
//    is_over:      true,   // overriden when building
//    created_at:   Fri Apr 15 2011 12:02:25 GMT-0400 (EDT),
//    random_seed:  0.8999513240996748,
//    players: [
//                {id: 1, name:'Player 1'},
//                {id: 2, name:'Player 2'}
//    ]
//}

For a factory with a constructor, if you want just the attributes:

Factory.attributes('game'); // return just the attributes

Programmatic Generation of Attributes

You can specify options that are used to programmatically generate the attributes:

const moment = require('moment');

Factory.define('matches')
  .attr('seasonStart', '2016-01-01')
  .option('numMatches', 2)
  .attr('matches', ['numMatches', 'seasonStart'], (numMatches, seasonStart) => {
    const matches = [];
    for (const i = 1; i <= numMatches; i++) {
      matches.push({
        matchDate: moment(seasonStart).add(i, 'week').format('YYYY-MM-DD'),
        homeScore: Math.floor(Math.random() * 5),
        awayScore: Math.floor(Math.random() * 5),
      });
    }
    return matches;
  });

Factory.build('matches', { seasonStart: '2016-03-12' }, { numMatches: 3 });
// Built object (note scores are random):
//{
//  seasonStart: '2016-03-12',
//  matches: [
//    { matchDate: '2016-03-19', homeScore: 3, awayScore: 1 },
//    { matchDate: '2016-03-26', homeScore: 0, awayScore: 4 },
//    { matchDate: '2016-04-02', homeScore: 1, awayScore: 0 }
//  ]
//}

In the example numMatches is defined as an option, not as an attribute. Therefore numMatches is not part of the output, it is only used to generate the matches array.

In the same example seasonStart is defined as an attribute, therefore it appears in the output, and can also be used in the generator function that creates the matches array.

Batch Specification of Attributes

The convenience function attrs simplifies the common case of specifying multiple attributes in a batch. Rewriting the game example from above:

Factory.define('game')
  .sequence('id')
  .attrs({
    is_over: false,
    created_at: () => new Date(),
    random_seed: () => Math.random(),
  })
  .attr('players', ['players'], (players) => {
    /* etc. */
  });

Post Build Callback

You can also define a callback function to be run after building an object:

Factory.define('coach')
  .option('buildPlayer', false)
  .sequence('id')
  .attr('players', ['id', 'buildPlayer'], (id, buildPlayer) => {
    if (buildPlayer) {
      return [Factory.build('player', { coach_id: id })];
    }
  })
  .after((coach, options) => {
    if (options.buildPlayer) {
      console.log('built player:', coach.players[0]);
    }
  });

Factory.build('coach', {}, { buildPlayer: true });

Multiple callbacks can be registered, and they will be executed in the order they are registered. The callbacks can manipulate the built object before it is returned to the callee.

If the callback doesn't return anything, rosie will return build object as final result. If the callback returns a value, rosie will use that as final result instead.

Associate a Factory with an existing Class

This is an advanced use case that you can probably happily ignore, but store this away in case you need it.

When you define a factory you can optionally provide a class definition, and anything built by the factory will be passed through the constructor of the provided class.

Specifically, the output of .build is used as the input to the constructor function, so the returned object is an instance of the specified class:

class SimpleClass {
  constructor(args) {
    this.moops = 'correct';
    this.args = args;
  }

  isMoopsCorrect() {
    return this.moops;
  }
}

testFactory = Factory.define('test', SimpleClass).attr('some_var', 4);

testInstance = testFactory.build({ stuff: 2 });
console.log(JSON.stringify(testInstance, {}, 2));
// Output:
// {
//   "moops": "correct",
//   "args": {
//     "stuff": 2,
//     "some_var": 4
//   }
// }

console.log(testInstance.isMoopsCorrect());
// Output:
// correct

Mind. Blown.

Usage in Node.js

To use Rosie in node, you'll need to import it first:

import { Factory } from 'rosie';
// or with `require`
const Factory = require('rosie').Factory;

You might also choose to use unregistered factories, as it fits better with node's module pattern:

// factories/game.js
import { Factory } from 'rosie';

export default new Factory().sequence('id').attr('is_over', false);
// etc

To use the unregistered Game factory defined above:

import Game from './factories/game';

const game = Game.build({ is_over: true });

A tool like babel is currently required to use this syntax.

You can also extend an existing unregistered factory:

// factories/scored-game.js
import { Factory } from 'rosie';
import Game from './game';

export default new Factory().extend(Game).attrs({
  score: 10,
});

Rosie API

As stated above the rosie factory signatures can be broken into factory definition and object creation.

Additionally factories can be defined and accessed via the Factory singleton, or they can be created and maintained by the callee.

Factory declaration functions

Once you have an instance returned from a Factory.define or a new Factory() call, you do the actual of work of defining the objects. This is done using the methods below (note these are typically chained together as in the examples above):

Factory.define

instance.attr:

Use this to define attributes of your objects

instance.attrs:

Use this as a convenience function instead of calling instance.attr multiple times

See instance.attr above for details. Note: there is no way to specify dependencies using this method, so if you need that, you should use instance.attr instead.

instance.option:

Use this to define options. Options do not appear in the generated object, but they can be used in a generator_function that is used to configure an attribute or sequence that appears in the generated object. See the Programmatic Generation Of Attributes section for examples.

instance.sequence:

Use this to define an auto incrementing sequence field in your object

instance.after:

Object building functions

build

Returns an object that is generated by the named factory. attributes and options are optional parameters. The factory_name is required when calling against the rosie Factory singleton.

buildList

Identical to .build except it returns an array of built objects. size is required, attributes and options are optional

Testing

You may find resetAll useful when working with testing frameworks such as Jest. It resets any build state, such as sequences, to their original values:

import Factory from 'rosie';

beforeEach(() => {
  Factory.resetAll();
});

Or call reset on a specific factory:

import Game from './game';

beforeEach(() => {
  Game.reset();
});

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Install the test dependencies (npm install - requires NodeJS)
  4. Make your changes and make sure the tests pass (npm test)
  5. Commit your changes (git commit -am 'Added some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new Pull Request

Credits

Thanks to Daniel Morrison for the name and Jon Hoyt for inspiration and brainstorming the idea.