This directory contains comprehensive examples demonstrating various features and use cases of Interface-Forge.
Learn the fundamentals of Interface-Forge:
- Creating simple factories
- Building single instances with
build() - Generating batches with
batch() - Overriding default values
- Using Faker methods directly
Explore factory composition techniques:
- Composing multiple factories together
- Creating complex object graphs
- Using
compose()for factory composition - Working with
iterate()for progressive data - Building relationships between entities
See how Interface-Forge integrates with testing frameworks:
- Unit test scenarios
- Integration test patterns
- Jest/Vitest test examples
- Storybook story data generation
- Snapshot testing approaches
Handle complex data structures with circular dependencies:
- Using
use()for lazy evaluation - Managing bi-directional relationships
- Controlling recursion depth with
maxDepth - Building self-referential structures
- Creating graph-like data models
Advanced techniques for sophisticated scenarios:
- State-based factories
- Using
sample()for controlled randomness - Temporal data patterns
- Weighted distributions
- Deterministic data with seeds
Learn how to use hooks for data transformation and validation:
- Using
beforeBuild()andafterBuild()hooks - Synchronous hooks with
build() - Asynchronous hooks with
buildAsync() - Data validation and transformation
- Conditional logic in hooks
- Error handling and business rules
Integrate Interface-Forge with Zod schemas across three examples:
- Creating factories from Zod schemas
- Simple schema examples
- Batch generation with overrides
- API response mocking
- Complex nested schemas
- Custom generators for specific fields
- Discriminated unions and recursive schemas
- Hooks and transformations
- Working with Maps, Sets, and Records
- Generating test data from domain schemas
- Creating test scenarios
- Mock API responses
- Integration with test frameworks
Advanced factory functions that inspect kwargs to make decisions:
- Use kwargs to create more realistic and consistent data
- Pass context to factories and receive consistent data
To run any example:
# Install dependencies
npm install interface-forge
# For Zod examples, also install Zod
npm install zod
# Run with TypeScript
npx tsx examples/01-basic-usage.ts
# Or compile and run
npx tsc examples/01-basic-usage.ts --outDir dist
node dist/01-basic-usage.jsFor Zod examples, import from the separate entry point:
import { ZodFactory } from 'interface-forge/zod';Every factory is created by instantiating the Factory class with a function that returns your data structure:
const MyFactory = new Factory<MyType>((faker) => ({
// Use faker methods to generate data
}));Since Factory extends Faker, the parameter passed to your factory function is the factory instance itself, giving you access to all Faker.js methods plus Interface-Forge specific methods like use(), sample(), etc.
All examples are written in TypeScript to demonstrate the full type safety benefits. The generated data will always match your defined interfaces.
Have an interesting use case or pattern? Feel free to contribute additional examples by submitting a pull request!