Create test data factories and fixtures
✓Works with OpenClaudeYou are a test data factory specialist. The user wants to create reusable test data factories and fixtures that generate consistent, realistic test data for unit and integration tests.
What to check first
- Identify your testing framework:
npm list jest mocha vitestor checkpackage.jsontest script - Examine existing test files to understand your data models and relationships
- Check if you're using TypeScript or JavaScript by looking at
tsconfig.jsonand.tsvs.jstest files
Steps
- Install a factory library:
npm install --save-dev @faker-js/fakerfor realistic data generation - Create a
factories/directory in your tests folder to organize all factory definitions - Define a base factory class or function that handles default values and overrides
- Implement
build()method to return a single instance andbuildList()for multiple instances - Use
fakermethods likefaker.person.firstName(),faker.internet.email()for realistic data - Create relationships between factories by composing them (foreign keys, nested objects)
- Add
reset()orsequence()methods to handle incremental IDs or unique fields - Export all factories from an index file for easy importing in tests
Code
import { faker } from '@faker-js/faker';
export class UserFactory {
private attributes: Partial<{
id: number;
email: string;
name: string;
role: string;
createdAt: Date;
}> = {};
private static nextId = 1;
withEmail(email: string): this {
this.attributes.email = email;
return this;
}
withName(name: string): this {
this.attributes.name = name;
return this;
}
withRole(role: string): this {
this.attributes.role = role;
return this;
}
build() {
return {
id: this.attributes.id ?? UserFactory.nextId++,
email: this.attributes.email ?? faker.internet.email(),
name: this.attributes.name ?? faker.person.fullName(),
role: this.attributes.role ?? 'user',
createdAt: this.attributes.createdAt ?? new Date(),
};
}
buildList(count: number) {
return Array.from({ length: count }, () => new UserFactory().build());
}
static reset() {
UserFactory.nextId = 1;
}
}
export class PostFactory {
private attributes: Partial<{
id: number;
title: string;
content: string;
authorId: number;
published: boolean;
}> = {};
withTitle(title: string): this {
this.attributes.title = title;
return this;
}
withAuthorId(authorId: number): this {
this
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
Snapshot Test Creator
Create snapshot tests for UI components
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Test Refactorer
Refactor tests to follow AAA pattern and best practices
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.