{"library":"rosie","title":"Rosie Factory for Test Data","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install rosie"],"cli":null},"imports":["import { Factory } from 'rosie'; // For ESM environments","const { Factory } = require('rosie'); // For CommonJS environments","/// <reference types=\"rosie\" />\nimport { Factory } from 'rosie'; // For TypeScript"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Factory } from 'rosie';\n\n// Define a 'game' factory with sequences, attributes, and dependent attributes.\nFactory.define('game')\n  .sequence('id')\n  .attr('is_over', false)\n  .attr('created_at', () => new Date())\n  .attr('random_seed', () => Math.random())\n  .attr('players', ['players'], (players) => {\n    if (!players) {\n      players = [{}, {}];\n    }\n    return players.map((data) => Factory.attributes('player', data));\n  });\n\n// Define a 'player' factory, extended by 'disabled-player'.\nFactory.define('player')\n  .sequence('id')\n  .sequence('name', (i) => `player${i}`)\n  .attr('position', ['id'], (id) => {\n    const positions = ['pitcher', '1st base', '2nd base', '3rd base'];\n    return positions[id % positions.length];\n  });\n\n// Extend the 'player' factory to create a 'disabled-player'.\nFactory.define('disabled-player').extend('player').attr('state', 'disabled');\n\n// Build an object, overriding some attributes.\nconst game = Factory.build('game', { is_over: true });\nconsole.log('Built Game:', game);\n\n// Build an object using the extended factory.\nconst disabledPlayer = Factory.build('disabled-player');\nconsole.log('Built Disabled Player:', disabledPlayer);\n\n// Get just the attributes without invoking a constructor.\nconst gameAttributes = Factory.attributes('game', { is_over: false });\nconsole.log('Game Attributes:', gameAttributes);\n\n// Example with options (options are not part of the final object).\nFactory.define('matches')\n  .attr('seasonStart', '2024-01-01')\n  .option('numMatches', 2)\n  .attr('matches', ['numMatches', 'seasonStart'], (numMatches, seasonStart) => {\n    const matches = [];\n    for (let i = 1; i <= numMatches; i++) {\n      const matchDate = new Date(seasonStart);\n      matchDate.setDate(matchDate.getDate() + (i * 7)); // Add weeks\n      matches.push({\n        matchDate: matchDate.toISOString().split('T')[0],\n        homeScore: Math.floor(Math.random() * 5),\n        awayScore: Math.floor(Math.random() * 5)\n      });\n    }\n    return matches;\n  });\n\nconst matchData = Factory.build('matches', { seasonStart: '2024-03-12' }, { numMatches: 3 });\nconsole.log('Match Data with options:', matchData);\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}