CommandsIntermediate4-6 min reference
Faker.js
Faker (@faker-js/faker) generates realistic fake data — names, emails, addresses, dates — so tests aren't littered with "test1", "test2". This is a quick lookup of the common generators and the one setting that matters most for stable tests (seeding). For when to use it versus fixtures, see Test Data Patterns.
Setup
import { faker } from "@faker-js/faker";
// locale-specific:
import { fakerDE as faker } from "@faker-js/faker";Common generators
| Call | Returns |
|---|---|
faker.person.fullName() | "Jane Doe" |
faker.internet.email() | "jane.doe@example.com" |
faker.internet.userName() | "jane_doe" |
faker.phone.number() | a phone number |
faker.location.streetAddress() | a street address |
faker.date.past() / future() / recent() | a Date |
faker.number.int({ min, max }) | an integer |
faker.string.uuid() | a UUID |
faker.helpers.arrayElement([...]) | a random item |
faker.helpers.multiple(fn, { count }) | an array of generated items |
faker.commerce.price() / product() | commerce data |
Determinism — seed it
faker.seed(123); // same sequence every runWithout a seed, a failing test isn't reproducible. Seed in a global setup (or per test) so CI and local match.
Tips
- Use
faker.helpers.unique(fn)cautiously (deprecated in newer versions — prefer your own uniqueness, e.g. append a counter/uuid). - Set a locale to match the system under test (validation, formatting).
- Generate via factories that call Faker, not Faker calls scattered through tests.
Common mistakes
- Not seeding → non-reproducible failures.
- Relying on uniqueness without guaranteeing it (collisions cause flake).
- Generating data that violates the app's validation (wrong locale/format).
- Faker calls inline everywhere instead of behind a factory.
// Related resources