Skip to content

[QA-02] Add Jest unit and integration tests for all backend services #1052

Description

@yusuftomilola

Overview

Backend services currently have little to no test coverage. Changes to business logic in AssetsService, AuthService, or ReportsService can introduce bugs that only surface in production. This issue establishes minimum coverage targets and writes tests for the highest-risk services.

Context

  • NestJS ships with Jest configuration out of the box — package.json already has the test script
  • Unit tests should mock the Repository using @golevelup/ts-jest or TypeORM's createMockRepository pattern
  • Integration tests (for services that hit the DB) should use a test PostgreSQL database via testcontainers-node
  • Coverage threshold: minimum 70% line coverage per service file

Acceptance Criteria

  • Configure Jest coverage thresholds in package.json jest config: branches: 70, lines: 70, functions: 80
  • Write unit tests for AuthService: register (duplicate email → 409), login (wrong password → 401), token refresh (expired token → 401)
  • Write unit tests for AssetsService: create asset (valid DTO creates record), update (returns updated data), delete (soft-deletes, audit log created)
  • Write unit tests for RolesGuard: ADMIN passes, USER blocked on ADMIN route (403), MANAGER passes on MANAGER route
  • Write integration test for AssetsService.findAll(): MANAGER only sees own-department assets, ADMIN sees all
  • Add test:cov script that fails CI if coverage drops below thresholds

Metadata

Metadata

Assignees

No one assigned

    Labels

    testingTests, QA, and quality assurance

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions