Your task is to write unit tests for the Knowledge Hub API using Vitest testing framework.
This is a continuation of the previous assignments. You will work in the same repository created in assignment 05.
Note: Integration tests for the Knowledge Hub API are provided by the course and have been running since assignment
05. Your responsibility is to ensure that your changes never break the existing integration test suite. In this assignment, you focus exclusively on unit testing the application.
- Use Vitest as the testing framework
- Use
@nestjs/testingfor module instantiation in unit tests - Use 24.x.x version (24.10.0 or upper) of Node.js
-
Services
Write unit tests for all service classes. Each service must be tested in isolation — all dependencies (database, external services) must be mocked.
- User service: signup data validation, password hashing, role assignment, user not found, duplicate login
- Article service: article creation validation, status transitions (
draft → published → archived), invalid transitions, tag management, filtering logic (status,categoryId,tag) - Auth service: JWT token generation, token verification, refresh token rotation, expired/invalid token handling, RBAC permission checks
-
Guards
Write unit tests for all custom guards:
- JWT Auth Guard: valid token passes, missing/malformed/expired token throws
UnauthorizedException - Roles Guard: correct role grants access, insufficient role throws
ForbiddenException, missing@Roles()metadata defaults correctly
- JWT Auth Guard: valid token passes, missing/malformed/expired token throws
-
Pipes
Write unit tests for any custom pipes:
- ParseUUID pipe (or equivalent): valid UUID passes through, invalid string throws
BadRequestException
- ParseUUID pipe (or equivalent): valid UUID passes through, invalid string throws
-
Interceptors & Filters
- Response interceptors (e.g. password stripping from User responses): verify the field is absent in output
- Exception filters (if custom): verify the correct HTTP status and error shape is returned for known exceptions
-
DTO Validation
Test that DTO classes with
class-validatordecorators behave correctly:- Required fields missing → validation fails
- Invalid enum values → validation fails
- Valid payload → validation passes
Use
validateOrReject/validatefromclass-validatordirectly — no HTTP layer needed. -
Mocking
- Mock the data access layer (Prisma Client or repository layer) in all unit tests to isolate business logic from the database
- Use Vitest's built-in mocking capabilities (
vi.mock,vi.fn,vi.spyOn) - Do not perform any real database calls or HTTP requests in unit tests
-
Edge Cases
Cover the following edge cases explicitly:
- Invalid or malformed UUIDs
- Missing required fields
- Duplicate user login/signup
- Expired or tampered JWT tokens
- Operations forbidden by role (e.g. viewer trying to create/delete)
- Accessing or modifying a non-existent resource
-
NPM Scripts
npm run test— runs all testsnpm run test:unit— runs only unit testsnpm run test:coverage— runs all tests with coverage report
-
Coverage Thresholds
Configure coverage thresholds in
vitest.config.ts. Tests must meet:- Lines: ≥ 90%
- Branches: ≥ 85%
npm run test:coveragemust exit with a non-zero code if thresholds are not met. -
Test Configuration
- Create a
vitest.config.tswith proper settings - Tests should be placed in
__tests__/directories or in files with.test.ts/.spec.tssuffix - Unit tests should live separately from any integration tests (e.g.
src/**/*.unit.spec.tsorsrc/__tests__/unit/)
- Create a