Rosie Factory for Test Data

2.1.1 · active · verified Tue Apr 21

Rosie is a JavaScript library designed to create factories for building complex JavaScript objects, primarily for setting up test data. Inspired by Ruby's `factory_bot` (formerly `factory_girl`), it simplifies the generation of structured data. The current stable version is 2.1.1. Key features include defining factories with attributes, sequences for unique values, options to programmatically generate attributes without including them in the final object, and callbacks for post-build processing. Factories can also inherit from others, promoting reusability. Rosie differentiates itself by offering a flexible, declarative way to construct objects, managing inter-attribute dependencies and providing fine-grained control over the generated data structure, making it highly suitable for unit and integration testing where consistent, yet varied, data is required. There is no explicit release cadence, but the project maintains a stable API.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining multiple factories, including inheritance, using sequences, dependent attributes, overriding attributes during build, and utilizing options that influence object creation without being part of the final output. It covers the core `define`, `build`, and `attributes` methods.

import { Factory } from 'rosie';

// Define a 'game' factory with sequences, attributes, and dependent attributes.
Factory.define('game')
  .sequence('id')
  .attr('is_over', false)
  .attr('created_at', () => new Date())
  .attr('random_seed', () => Math.random())
  .attr('players', ['players'], (players) => {
    if (!players) {
      players = [{}, {}];
    }
    return players.map((data) => Factory.attributes('player', data));
  });

// Define a 'player' factory, extended by 'disabled-player'.
Factory.define('player')
  .sequence('id')
  .sequence('name', (i) => `player${i}`)
  .attr('position', ['id'], (id) => {
    const positions = ['pitcher', '1st base', '2nd base', '3rd base'];
    return positions[id % positions.length];
  });

// Extend the 'player' factory to create a 'disabled-player'.
Factory.define('disabled-player').extend('player').attr('state', 'disabled');

// Build an object, overriding some attributes.
const game = Factory.build('game', { is_over: true });
console.log('Built Game:', game);

// Build an object using the extended factory.
const disabledPlayer = Factory.build('disabled-player');
console.log('Built Disabled Player:', disabledPlayer);

// Get just the attributes without invoking a constructor.
const gameAttributes = Factory.attributes('game', { is_over: false });
console.log('Game Attributes:', gameAttributes);

// Example with options (options are not part of the final object).
Factory.define('matches')
  .attr('seasonStart', '2024-01-01')
  .option('numMatches', 2)
  .attr('matches', ['numMatches', 'seasonStart'], (numMatches, seasonStart) => {
    const matches = [];
    for (let i = 1; i <= numMatches; i++) {
      const matchDate = new Date(seasonStart);
      matchDate.setDate(matchDate.getDate() + (i * 7)); // Add weeks
      matches.push({
        matchDate: matchDate.toISOString().split('T')[0],
        homeScore: Math.floor(Math.random() * 5),
        awayScore: Math.floor(Math.random() * 5)
      });
    }
    return matches;
  });

const matchData = Factory.build('matches', { seasonStart: '2024-03-12' }, { numMatches: 3 });
console.log('Match Data with options:', matchData);

view raw JSON →