Adding Fake Data to the Kawkab Project
Overview
Kawkab makes it easy to generate fake data using the Faker.js library, a powerful tool for generating realistic fake data for testing and development purposes. This document explains how to use Faker within the Kawkab framework.
Basic Usage
import { faker } from 'kawkab';
// Basic example for generating fake data
const user = {
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
avatar: faker.image.avatar(),
createdAt: faker.date.past()
};
Common Use Cases
Generating User Data
const generateUser = () => ({
id: faker.string.uuid(),
username: faker.internet.userName(),
email: faker.internet.email(),
password: faker.internet.password(),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
phone: faker.phone.number(),
address: {
street: faker.location.street(),
city: faker.location.city(),
country: faker.location.country(),
zipCode: faker.location.zipCode()
}
});
Generating Product Data
const generateProduct = () => ({
id: faker.string.uuid(),
name: faker.commerce.productName(),
price: faker.commerce.price(),
description: faker.commerce.productDescription(),
category: faker.commerce.department(),
image: faker.image.url()
});
Date and Time
const generateTimeData = () => ({
past: faker.date.past(),
future: faker.date.future(),
recent: faker.date.recent(),
timestamp: faker.date.timestamp()
});
Best Practices
-
Seeding for Consistency: Use
faker.seed()
for consistent results across testsfaker.seed(123);
-
Create Factory Functions: Organize fake data generation into factory functions
export const userFactory = (overrides = {}) => ({ ...generateUser(), ...overrides });
-
Type Safety: Use TypeScript interfaces with your factories
interface User { id: string; username: string; email: string; } const userFactory = (overrides: Partial<User> = {}): User => ({ ...generateUser(), ...overrides });
Common Categories
- Person:
faker.person.*
- Generate names, titles, etc. - Internet:
faker.internet.*
- Generate emails, usernames, URLs, etc. - Date:
faker.date.*
- Generate dates and timestamps - Commerce:
faker.commerce.*
- Generate product data and prices - Company:
faker.company.*
- Generate company names and data - Image:
faker.image.*
- Generate image URLs - Location:
faker.location.*
- Generate addresses and coordinates
Example Test Implementation
import { faker } from 'kawkab';
describe('User Service Tests', () => {
it('should create a new user', async () => {
const mockUser = {
username: faker.internet.userName(),
email: faker.internet.email(),
password: faker.internet.password()
};
// Run your test here
});
});
Testing Tips
- Use
beforeEach
to reset faker data if needed - Create specific factories for different test scenarios
- Use faker’s seeding feature for repeatable tests
- Combine multiple faker methods for complex data structures
Important Notes
- Make sure to use the generated fake data in your fields.
- Regularly update your fake data to reflect changes in your data model.
- Use comments to explain the purpose of fake data.
- Avoid using fake data in production environments.
Additional Examples
Generating Article Data
const generateArticle = () => ({
id: faker.string.uuid(),
title: faker.lorem.sentence(),
content: faker.lorem.paragraphs(),
author: faker.person.fullName(),
tags: Array.from({ length: 3 }, () => faker.lorem.word()),
publishDate: faker.date.recent()
});
Generating Comment Data
const generateComment = () => ({
id: faker.string.uuid(),
content: faker.lorem.paragraph(),
author: faker.person.fullName(),
createdAt: faker.date.recent(),
likes: faker.number.int({ min: 0, max: 100 })
});