Why Fake Data Quality Matters
Tests written with name: "John" and email: "test@test.com" only test the happy path. Real users have names with apostrophes (O'Brien), long email addresses, accented characters, hyphenated surnames, and edge-case values. Realistic fake data catches bugs that contrived test fixtures never expose.
What to Use Faker For
- Seeding development and staging databases with realistic-looking data
- Generating mock API responses for frontend development before the API is ready
- Property-based testing — generate random valid inputs and verify invariants hold
- Load testing — fill a database with thousands of records to find performance issues
- Demo environments — show the product with realistic-looking content
Faker.js Patterns
import { faker } from "@faker-js/faker";
// Basic usage
faker.person.fullName() // "Johnathan Kiehn"
faker.internet.email() // "clarice82@gmail.com"
faker.phone.number() // "+1-555-867-5309"
faker.company.name() // "Acme Corp"
faker.location.city() // "San Francisco"
faker.date.past() // Date object, sometime in the past year
faker.number.int({ min: 1, max: 100 }) // random integer
// Generate a realistic user object
function fakeUser() {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
return {
id: faker.string.uuid(),
name: `${firstName} ${lastName}`,
email: faker.internet.email({ firstName, lastName }),
avatar: faker.image.avatar(),
createdAt: faker.date.past({ years: 2 }).toISOString(),
plan: faker.helpers.arrayElement(["free", "pro", "enterprise"]),
active: faker.datatype.boolean(0.8), // 80% chance true
};
}
// Generate an array of 20 users
const users = faker.helpers.multiple(fakeUser, { count: 20 });
Reproducible Fake Data with Seeds
// Set a seed for reproducible output
faker.seed(12345);
faker.person.fullName(); // always "Christiana Wintheiser" with this seed
// In tests — seed before each test for determinism
beforeEach(() => faker.seed(42));
Seeding a Database
// prisma/seed.ts
import { PrismaClient } from "@prisma/client";
import { faker } from "@faker-js/faker";
const prisma = new PrismaClient();
async function main() {
faker.seed(1); // reproducible
const users = Array.from({ length: 50 }, () => ({
email: faker.internet.email(),
name: faker.person.fullName(),
createdAt: faker.date.past(),
}));
await prisma.user.createMany({ data: users });
}
main().finally(() => prisma.$disconnect());
Python: Faker Library
from faker import Faker
fake = Faker()
fake.name() # "Dr. Amanda Howell"
fake.email() # "mwright@example.com"
fake.address() # "123 Main St\nSpringfield, IL 62701"
fake.json() # random JSON structure
# Localized data
fake_de = Faker("de_DE")
fake_de.name() # "Karl-Heinz Müller"
Generate Mock JSON Instantly
Use ToolsVito's Mock Data Generator to create fake JSON test data with custom schemas — configure field types and generate arrays of records in your browser.