Core Concepts

Entity relationships

Model and generate interconnected entities using builder and decorators

Defining base entities

Use decorators to declare how fields are generated.

class Product {
  @FactoryValue(faker => faker.commerce.productName())
  name: string;
 
  @FactoryValue(faker => Number(faker.commerce.price()))
  price: number;
}
 
class Customer {
  @FactoryValue(faker => faker.internet.email())
  email: string;
 
  @FactoryValue(faker => faker.person.fullName())
  fullName: string;
}

Establishing relations

Use @FactoryType to link entities.

class Order {
  @FactoryValue(faker => faker.number.int())
  id: number;
 
  @FactoryType(() => Customer)
  customer: Customer;
 
  @FactoryType(() => Product)
  product: Product;
}

Generating instances

Instantiate connected objects by opting in with .with().

import { Factory } from 'decorated-factory';
import { faker } from '@faker-js/faker';
 
const factory = new Factory(faker);
const order = factory.one(Order)
  .with('customer')
  .with('product')
  .make();
 
console.log(order.customer.fullName);
console.log(order.product.name);

On this page