🧪 Testing

Mocking Strategies

Effective mocking patterns for Gauzy tests.

Repository Mocking

const mockRepository = {
  findOne: jest.fn(),
  find: jest.fn(),
  save: jest.fn(),
  delete: jest.fn(),
  count: jest.fn(),
  createQueryBuilder: jest.fn(() => ({
    where: jest.fn().mockReturnThis(),
    andWhere: jest.fn().mockReturnThis(),
    getMany: jest.fn().mockResolvedValue([]),
    getCount: jest.fn().mockResolvedValue(0),
  })),
};

Service Mocking

const mockEmployeeService = {
  findAll: jest.fn().mockResolvedValue({ items: [], total: 0 }),
  findOneByIdString: jest.fn(),
  create: jest.fn(),
  update: jest.fn(),
  delete: jest.fn(),
};

// In module
providers: [{ provide: EmployeeService, useValue: mockEmployeeService }];

HTTP Mocking

// Angular HTTP testing
import {
  HttpClientTestingModule,
  HttpTestingController,
} from "@angular/common/http/testing";

let httpMock: HttpTestingController;

it("should fetch employees", () => {
  service.getEmployees().subscribe((data) => {
    expect(data.length).toBe(2);
  });

  const req = httpMock.expectOne("/api/employee");
  req.flush([{ id: "1" }, { id: "2" }]);
});

Request Context Mocking

const mockRequest = {
  user: { id: "user-uuid", tenantId: "tenant-uuid" },
  headers: { authorization: "Bearer token" },
};

const mockRequestContext = {
  currentTenantId: jest.fn().mockReturnValue("tenant-uuid"),
  currentUserId: jest.fn().mockReturnValue("user-uuid"),
};

When to Mock vs. Use Real

Use MockUse Real
External HTTP APIsDatabase (in integration)
Email servicesValidation pipes
File storage providersDTO transformations
Payment gatewaysBusiness logic calculations

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey