Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Testingintermediate

Test Data Factory

Share

Create test data factories and fixtures

Works with OpenClaude

You 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 vitest or check package.json test script
  • Examine existing test files to understand your data models and relationships
  • Check if you're using TypeScript or JavaScript by looking at tsconfig.json and .ts vs .js test files

Steps

  1. Install a factory library: npm install --save-dev @faker-js/faker for realistic data generation
  2. Create a factories/ directory in your tests folder to organize all factory definitions
  3. Define a base factory class or function that handles default values and overrides
  4. Implement build() method to return a single instance and buildList() for multiple instances
  5. Use faker methods like faker.person.firstName(), faker.internet.email() for realistic data
  6. Create relationships between factories by composing them (foreign keys, nested objects)
  7. Add reset() or sequence() methods to handle incremental IDs or unique fields
  8. 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

Quick Info

CategoryTesting
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
testingfixturesdata

Install command:

curl -o ~/.claude/skills/test-data-factory.md https://claude-skills-hub.vercel.app/skills/testing/test-data-factory.md

Related Testing Skills

Other Claude Code skills in the same category — free to download.

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.