# Test Fixtures & Factories {#test-fixtures--factories}


Create and manage test data for consistent, reliable tests.


## Factory Pattern {#factory-pattern}


Create reusable factories for entity generation:

```typescript
// factories/employee.factory.ts
import { faker } from "@faker-js/faker";

export const createMockEmployee = (overrides = {}) => ({
  id: faker.string.uuid(),
  firstName: faker.person.firstName(),
  lastName: faker.person.lastName(),
  email: faker.internet.email(),
  startedWorkOn: faker.date.past().toISOString(),
  isActive: true,
  tenantId: faker.string.uuid(),
  organizationId: faker.string.uuid(),
  ...overrides,
});

export const createMockTask = (overrides = {}) => ({
  id: faker.string.uuid(),
  title: faker.lorem.sentence(4),
  description: faker.lorem.paragraph(),
  status: "TODO",
  priority: "MEDIUM",
  ...overrides,
});
```


## Using Factories {#using-factories}


```typescript
describe("EmployeeService", () => {
  it("should calculate total hours", () => {
    const employee = createMockEmployee({ isActive: true });
    const timeLogs = [
      createMockTimeLog({ employeeId: employee.id, duration: 3600 }),
      createMockTimeLog({ employeeId: employee.id, duration: 7200 }),
    ];

    const total = service.calculateTotalHours(timeLogs);
    expect(total).toBe(3); // 10800 seconds = 3 hours
  });
});
```


## Database Seeding for Tests {#database-seeding-for-tests}


```typescript
// test/seed.ts
export async function seedTestData(connection: Connection) {
  const tenant = await connection.getRepository(Tenant).save({
    name: "Test Tenant",
  });

  const organization = await connection.getRepository(Organization).save({
    name: "Test Org",
    tenantId: tenant.id,
  });

  const user = await connection.getRepository(User).save({
    email: "test@ever.co",
    tenantId: tenant.id,
    hash: await bcrypt.hash("test123", 10),
  });

  return { tenant, organization, user };
}
```


## Fixture Files {#fixture-files}


For static test data, use JSON fixtures:

```json
// fixtures/employees.json
[
  { "firstName": "John", "lastName": "Doe", "email": "john@test.co" },
  { "firstName": "Jane", "lastName": "Smith", "email": "jane@test.co" }
]
```


## Related Pages {#related-pages}


- [Unit Testing Guide](https://sourcey-gauzy.pages.dev/testing/unit-testing/index.md) — unit tests
- [Database Seeding](https://sourcey-gauzy.pages.dev/database/database-seeding/index.md) — production seeding
- [Mocking Strategies](https://sourcey-gauzy.pages.dev/testing/mocking-strategies/index.md) — mocking patterns
