xiaohongshu
xiaohongshu
generate-factory
Auto-generate test data factories from schemas, types, or models. Use when creating test data infrastructure, setting up fixtures, or reducing test setup boilerplate.
Full skill instructions
Auto-generate test data factories from database schemas, TypeScript interfaces, or model definitions. Factories produce realistic test data using Faker.js patterns, reducing test setup time by 60%.
| Pattern | Source | Reference |
|---|---|---|
| Factory Pattern | ThoughtBot | FactoryBot |
| Faker.js | Open Source | fakerjs.dev |
| Test Data Management | ISTQB | CT-TAS Test Automation Strategy |
| Synthetic Data | Tonic.ai | Faker Best Practices |
| Natural Language | Action |
|---|---|
| "Generate factory for User model" | Create user factory |
| "Create test data factories" | Generate factories for all models |
| "Add faker to tests" | Integrate faker with existing tests |
| "Make test data realistic" | Convert hard-coded to factory |
| "Generate fixtures from schema" | Schema-aware factory generation |
| Type | Purpose | When to Use |
|---|---|---|
| Factory | Generate dynamic test data | When you need many variations |
| Fixture | Static, predefined data | When exact values matter |
| Mock | Fake external dependencies | When isolating units |
// Basic factory
const user = userFactory.build();
// { id: 'uuid-1', name: 'John Doe', email: '[email protected]' }
// With overrides
const admin = userFactory.build({ role: 'admin' });
// { id: 'uuid-2', name: 'Jane Doe', email: '[email protected]', role: 'admin' }
// Build multiple
const users = userFactory.buildList(5);
// Array of 5 users
// With traits
const inactiveUser = userFactory.build({}, { trait: 'inactive' });
// { id: 'uuid-3', ..., status: 'inactive', deactivatedAt: Date }
// With relationships
const userWithOrders = userFactory.build({}, { with: ['orders'] });
// { id: 'uuid-4', ..., orders: [{ id: 'order-1', ... }] }
Detect schema/type from:
// Input: TypeScript interface
interface User {
id: string;
name: string;
email: string;
age: number;
role: 'admin' | 'user' | 'guest';
createdAt: Date;
preferences?: UserPreferences;
}
const TYPE_MAPPING = {
// Primitives
'string': 'faker.string.alphanumeric(10)',
'number': 'faker.number.int({ min: 0, max: 100 })',
'boolean': 'faker.datatype.boolean()',
'Date': 'faker.date.past()',
// Named fields (semantic mapping)
'id': 'faker.string.uuid()',
'name': 'faker.person.fullName()',
'email': 'faker.internet.email()',
'phone': 'faker.phone.number()',
'address': 'faker.location.streetAddress()',
'age': 'faker.number.int({ min: 18, max: 80 })',
'createdAt': 'faker.date.past()',
'updatedAt': 'faker.date.recent()',
// Enums
'role': 'faker.helpers.arrayElement(["admin", "user", "guest"])',
};
// Generated: factories/user.factory.ts
import { faker } from '@faker-js/faker';
import type { User } from '../types';
export const userFactory = {
/**
* Build a single User with optional overrides
*/
build: (overrides: Partial<User> = {}): User => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
age: faker.number.int({ min: 18, max: 80 }),
role: faker.helpers.arrayElement(['admin', 'user', 'guest']),
createdAt: faker.date.past(),
...overrides,
}),
/**
* Build multiple Users
*/
buildList: (count: number, overrides: Partial<User> = {}): User[] =>
Array.from({ length: count }, () => userFactory.build(overrides)),
/**
* Traits for common variations
*/
traits: {
admin: { role: 'admin' as const },
inactive: {
status: 'inactive',
deactivatedAt: faker.date.past(),
},
newUser: {
createdAt: faker.date.recent(),
},
},
/**
* Build with trait
*/
buildWithTrait: (trait: keyof typeof userFactory.traits, overrides: Partial<User> = {}): User =>
userFactory.build({ ...userFactory.traits[trait], ...overrides }),
};
For entities with relationships:
// factories/order.factory.ts
import { faker } from '@faker-js/faker';
import { userFactory } from './user.factory';
export const orderFactory = {
build: (overrides = {}) => ({
id: faker.string.uuid(),
userId: faker.string.uuid(),
items: [],
total: faker.number.float({ min: 10, max: 500, fractionDigits: 2 }),
status: faker.helpers.arrayElement(['pending', 'shipped', 'delivered']),
createdAt: faker.date.past(),
...overrides,
}),
/**
* Build with related user
*/
buildWithUser: (overrides = {}) => {
const user = userFactory.build();
return {
...orderFactory.build({ userId: user.id, ...overrides }),
user,
};
},
};
## Factory Generation Report
**Source**: `src/types/user.ts`
**Output**: `test/factories/user.factory.ts`
### Generated Factory
```typescript
import { faker } from '@faker-js/faker';
import type { User } from '../../src/types';
export const userFactory = {
build: (overrides: Partial<User> = {}): User => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
age: faker.number.int({ min: 18, max: 80 }),
role: faker.helpers.arrayElement(['admin', 'user', 'guest']),
createdAt: faker.date.past(),
preferences: null,
...overrides,
}),
buildList: (count: number, overrides: Partial<User> = {}): User[] =>
Array.from({ length: count }, () => userFactory.build(overrides)),
traits: {
admin: { role: 'admin' as const },
inactive: { status: 'inactive' },
},
};
| Field | Type | Faker Method |
|---|---|---|
| id | string | faker.string.uuid() |
| name | string | faker.person.fullName() |
| string | faker.internet.email() | |
| age | number | faker.number.int({ min: 18, max: 80 }) |
| role | enum | faker.helpers.arrayElement([...]) |
| createdAt | Date | faker.date.past() |
// Basic usage
const user = userFactory.build();
// With override
const admin = userFactory.build({ role: 'admin' });
// Multiple users
const users = userFactory.buildList(10);
// With trait
const inactive = userFactory.build(userFactory.traits.inactive);
{
"devDependencies": {
"@faker-js/faker": "^8.0.0"
}
}
## Deterministic Mode
For tests requiring reproducible data:
```typescript
// Set seed for reproducible data
import { faker } from '@faker-js/faker';
beforeEach(() => {
faker.seed(12345); // Same data every run
});
// Or per-factory
export const userFactory = {
buildDeterministic: (seed: number, overrides = {}) => {
faker.seed(seed);
return userFactory.build(overrides);
},
};
Generate factories for all models:
# From Prisma schema
npx generate-factory --source prisma/schema.prisma --output test/factories/
# From TypeScript types
npx generate-factory --source src/types/ --output test/factories/
tdd-enforce for test data requirementsGenerate factory from schema:
python scripts/factory_generator.py --source src/types/user.ts
Generate all factories:
python scripts/batch_generate.py --source src/types/ --output test/factories/
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience