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.
npm install rosieVerified import paths — ran on the pinned version, not inferred.
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.
For older Node.js environments, use CommonJS `require` syntax (`const { Factory } = require('rosie');`) or integrate a transpiler like Babel into your build process to handle ESM imports.Review the usage documentation for `.attr()` vs `.option()` carefully. Use `.attr()` for data that should appear in the final object, and `.option()` for configuration parameters that control how the object is built but are not part of its schema.
For isolated factory definitions, create factory instances directly: `const myFactory = new Factory().define('myModel');`. For global singleton cleanup between tests, ensure `Factory.reset()` is called in a `beforeEach` or `afterEach` hook in your test runner.Ensure you are importing `Factory` correctly: `const { Factory } = require('rosie');`.Verify 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.Correct the ESM import statement to use named import syntax: `import { Factory } from 'rosie';`.Ensure `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.No dependency data recorded yet.