Core Concepts

Basic usage

Getting started with builder syntax and decorators

Entity definition

import 'reflect-metadata';
import { Factory, FactoryValue } from 'decorated-factory';
import { faker } from '@faker-js/faker';
 
const factory = new Factory(faker);
 
class Product {
  @FactoryValue(faker => faker.number.int())
  id: number;
 
  @FactoryValue(faker => faker.commerce.productName())
  name: string;
}

Creating instances

// One instance
const product = factory.one(Product).make();
 
// Many instances
const products = factory.many(Product).make(5);

Overriding and excluding

Use the fluent builder to override or exclude fields. Call set/without after any relevant with() calls.

const customized = factory
  .one(Product)
  .set('name', 'Limited Edition Chair')
  .make();
 
const anonymous = factory
  .one(Product)
  .without('name')
  .make();

In v2, the old .new()/.create() APIs are replaced by a fluent builder: .one()/.many() with .with(), .set(), .without(), and .make()/.plain().

On this page