Objectory is a library for building nested object factories with strong typing. Use it to keep your test data builders consistent while still letting each test reshape the parts it cares about.
npm install -D @enormora/objectoryimport { createFactory } from '@enormora/objectory';
type Person = {
name: string;
age: number;
};
const personFactory = createFactory<Person>(() => {
return {
name: 'Jane Doe',
age: 32
};
});
const jane = personFactory.build();
const olderJane = personFactory.build({ age: 45 });
const crew = personFactory.buildList({ length: 3 });Objectory handles deeply nested factories, arrays, and targeted overrides so you can focus on the behaviours under test instead of wiring up objects.
const passengerFactory = createFactory(() => {
return {
name: 'Jane Doe',
age: 32
};
});
const tripFactory = createFactory(() => {
return {
driver: passengerFactory,
passengers: passengerFactory.asArray({ length: 2 })
};
});
const trip = tripFactory.build({
driver: { name: 'Alex' },
passengers: [{ age: 40 }]
});Create a factory from a generator function. The generator returns the canonical shape for the objects you want to build.
const personFactory = createFactory(() => {
return {
name: 'Jane Doe',
age: 32
};
});Build a single object, optionally overriding selected properties at any depth.
const adult = personFactory.build({ age: 21 });Build an array of identical instances by repeatedly calling build.
const passengers = personFactory.buildList({ length: 3 });Expose the factory as an array factory so it can be embedded in other factories.
const busFactory = createFactory(() => ({
passengers: personFactory.asArray({ length: 2 })
}));
const bus = busFactory.build();Create a new factory that always applies the given overrides before any ad-hoc overrides.
const namedFactory = personFactory.withOverrides({ name: 'Chris' });
const namedPerson = namedFactory.build();Derive a new factory by merging the base shape with additional fields from an extension generator.
const employeeFactory = personFactory.extend(() => ({
employeeId: 'E-001'
}));
const employee = employeeFactory.build();Build an object with the property at path removed, useful for negative tests.
const missingName = personFactory.buildInvalidWithout('name');Build an object with the property at path replaced by value, even if it breaks the schema.
const invalidAge = personFactory.buildInvalidWithChanged('age', 'unknown');Objectory is heavily inspired by the excellent cooky-cutter and fishery libraries — thank you for paving the way for ergonomic test data builders.