Rosie Factory for Test Data
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
-
ReferenceError: Factory is not defined
cause The `Factory` object was not correctly imported or made available in the current scope for CommonJS modules.fixEnsure you are importing `Factory` correctly: `const { Factory } = require('rosie');`. -
TypeError: Cannot read properties of undefined (reading 'define')
cause This usually means `Factory` was imported incorrectly in a CommonJS module, resulting in `undefined` or an unexpected object instead of the actual `Factory` instance with its methods.fixVerify the CommonJS import: `const { Factory } = require('rosie');`. If you used `const Factory = require('rosie');`, it will likely yield this error because the module's default export isn't the `Factory` itself, but an object containing `Factory` as a property. -
TypeError: The requested module 'rosie' does not provide an export named 'Factory'
cause This error occurs in an ESM context when attempting to import `Factory` as a default export (`import Factory from 'rosie';`) while it is a named export.fixCorrect the ESM import statement to use named import syntax: `import { Factory } from 'rosie';`. -
Error: Factory named 'myFactoryName' not found
cause You are trying to build an object from a factory that has not been defined or has been reset.fixEnsure `Factory.define('myFactoryName', ...)` has been called before attempting to `Factory.build('myFactoryName', ...)` or `Factory.attributes('myFactoryName', ...)`. If using `Factory.reset()`, ensure definitions are re-run before tests.
Warnings
- gotcha When using ESM `import` syntax in Node.js environments older than Node.js 12, a transpilation step (e.g., with Babel) might be necessary. While `engines.node >=10` is specified, native ESM support became stable later. Without transpilation, direct ESM imports might fail on Node.js 10/11.
- gotcha Distinguish carefully between `attr` and `option`. Attributes (`attr`) define properties that will be present in the final built object. Options (`option`) are parameters used *during* the object generation process but are *not* included in the resulting object. Misunderstanding this can lead to unexpected keys in your test data or missing data needed for generation.
- gotcha Rosie's default behavior involves a global `Factory` singleton. If you are running tests in parallel or in environments where global state can cause interference, definitions might conflict. To avoid this, consider using 'unregistered factories' or explicitly calling `Factory.reset()`.
Install
-
npm install rosie -
yarn add rosie -
pnpm add rosie
Imports
- Factory
import Factory from 'rosie'; // Rosie exports a named 'Factory', not a default.
import { Factory } from 'rosie'; // For ESM environments - Factory
const Factory = require('rosie'); // This would import the entire module object, not the Factory instance directly.const { Factory } = require('rosie'); // For CommonJS environments - Factory
import Factory from 'rosie'; // Incorrect default import for TypeScript
/// <reference types="rosie" /> import { Factory } from 'rosie'; // For TypeScript
Quickstart
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);