Faker is a NodeJS library that can be used to generate fake data.
It can be used to create dummy locations, names of people, avatars, products, sentences, phone numbers, dates and more.
To install FakerJS, simply use NPM:
You can now generate fake data, such as this example:
const { faker } = require('@faker-js/faker');
// or, if desiring a different locale
// const { fakerDE: faker } = require('@faker-js/faker');
const randomName = faker.person.fullName(); // Simon Coldwell
const randomEmail = faker.internet.email(); // stu.vermont@biznew.org
There's even support for multiple locales, so you can generate fake data in different languages.
FakerJS can easily be used with test frameworks such as Vitest and Jest, Playwright or Cypress.
For example, see this Playwright test code where we input a fake username and password in a form, using both FakerJS and Playwright.
import { faker } from '@faker-js/faker/locale/en'
import { expect, test } from '@playwright/test'
test.describe('Testing the application', () => {
test('should create an account with fake data', async ({
page,
}) => {
const username = faker.internet.userName();
const password = faker.internet.password();
const email = faker.internet.exampleEmail();
// Visit the webpage and create an account.
await page.goto('https://www.example.com/register');
await page.getByLabel('form_email').fill(email);
await page.getByLabel('form_user').fill(username);
await page.getByLabel('form_pass', { exact: true }).fill(password);
await page.getByRole('button', { name: 'Register' }).click();
});
});