Skip to content

Commit fa183e8

Browse files
fix: replace faker with native random helpers to fix ESM/CommonJS conflict
1 parent 08caa0e commit fa183e8

1 file changed

Lines changed: 35 additions & 10 deletions

File tree

src/shared/utils/DataFactory.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1-
import { faker } from '@faker-js/faker'
2-
import { TestUser, TestProduct } from '../types'
3-
import { ITestDataFactory } from '../../core/interfaces'
1+
import type { ITestDataFactory } from '../../core/interfaces'
2+
import type { TestUser, TestProduct } from '../types'
3+
4+
function randomString(length = 8): string {
5+
return Math.random().toString(36).substring(2, length + 2)
6+
}
7+
8+
function randomPrice(): number {
9+
return parseFloat((Math.random() * 999 + 1).toFixed(2))
10+
}
11+
12+
const firstNames = ['Alice', 'Bob', 'Carol', 'David', 'Eva', 'Frank', 'Grace', 'Henry']
13+
const lastNames = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller']
14+
const domains = ['gmail.com', 'yahoo.com', 'outlook.com', 'test.com']
15+
const categories = ['Electronics', 'Clothing', 'Books', 'Home', 'Sports', 'Toys']
16+
17+
function randomItem<T>(arr: T[]): T {
18+
return arr[Math.floor(Math.random() * arr.length)]
19+
}
20+
21+
function generateName(): string {
22+
return randomItem(firstNames) + ' ' + randomItem(lastNames)
23+
}
24+
25+
function generateEmail(name: string): string {
26+
return name.toLowerCase().replace(' ', '.') + randomString(4) + '@' + randomItem(domains)
27+
}
428

529
export class UserFactory implements ITestDataFactory<TestUser> {
630
create(overrides?: Partial<TestUser>): TestUser {
31+
const name = generateName()
732
return {
8-
name: faker.person.fullName(),
9-
email: faker.internet.email().toLowerCase(),
10-
password: 'Test@' + faker.internet.password({ length: 8 }),
33+
name,
34+
email: generateEmail(name),
35+
password: 'Test@' + randomString(8),
1136
role: 'user',
1237
...overrides,
1338
}
@@ -29,10 +54,10 @@ export class UserFactory implements ITestDataFactory<TestUser> {
2954
export class ProductFactory implements ITestDataFactory<TestProduct> {
3055
create(overrides?: Partial<TestProduct>): TestProduct {
3156
return {
32-
name: faker.commerce.productName(),
33-
price: parseFloat(faker.commerce.price({ min: 1, max: 1000 })),
34-
category: faker.commerce.department(),
35-
description: faker.commerce.productDescription(),
57+
name: 'Product ' + randomString(6),
58+
price: randomPrice(),
59+
category: randomItem(categories),
60+
description: 'Description for product ' + randomString(4),
3661
...overrides,
3762
}
3863
}

0 commit comments

Comments
 (0)