Skip to main content

Testing Guide

Testing across the platform follows a layered safety model. Each project uses its own framework and conventions, but all share the principle: tests must never target production systems or corrupt real data.

Backend (NestJS / Jest)

Test Commands

CommandSafetyWhat runs
npm run test:unitSafeUnit tests only (*.spec.ts)
npm run test:safeSafeUnit + mocked integration (recommended manual check before commit)
npm run test:integrationDangerousReal DB (requires RUN_REAL_INTEGRATION_TESTS=true)
npm run test:e2eDangerousReal API (requires RUN_REAL_E2E_TESTS=true + E2E_API_URL)

File Naming Conventions

SuffixLevelRan by CIDescription
*.spec.tsUnitYesMocked dependencies, no DB/API calls
*.integration.spec.tsSafe integrationYesTestPrismaService with auto-cleanup
*.real-integration.spec.tsReal DBNoRequires explicit env vars to run
*.e2e-spec.tsE2ENoHits real API endpoints

Mocking Patterns

// Service test with mocked Prisma
const module = await Test.createTestingModule({
providers: [
MyService,
{ provide: PrismaService, useValue: { user: { findUnique: jest.fn(), create: jest.fn() } } },
],
}).compile();

// Resolver test with guard override
const module = await Test.createTestingModule({ providers: [MyResolver, MyService] })
.overrideGuard(JwtAuthGuard)
.useValue({ canActivate: () => true })
.compile();

// Transaction mock
{ $transaction: jest.fn((fn) => fn(mockPrisma)) }

Safety Components

ComponentPurpose
TestEnvironmentGuardValidates NODE_ENV=test, blocks production DB URLs
E2EEnvironmentGuardPrevents targeting production endpoints
TestPrismaServicePrisma wrapper with built-in environment checks
TestDataManagerAutomatic test data tracking and cleanup

Background Tasks (FastAPI / pytest)

Test Commands

CommandScope
pytest -m unitUnit tests (mocked, sub-second)
pytest -m integrationReal DB with cleanup fixtures
pytest -m e2eE2E against ai-dev.aiqlick.com
pytest --cov=app --cov-report=term-missingWith coverage report

Conventions

  • Async mode: asyncio_mode = "auto" (all async tests auto-detected)
  • Coverage minimum: 30% (--cov-fail-under=30)
  • Markers: unit, integration, slow, e2e
  • Fixtures: db_pool, db_conn, clean_agent_tables in tests/conftest.py
  • Results: Auto-saved to docs/test-results/ via fixtures

Frontend (Next.js)

No test framework is configured. Build verification serves as the primary quality gate:

npm run build   # Must pass before commit

The pre-commit hook runs scripts/verify-build.sh which blocks commits on build failure.

Pre-Commit Hook Pipeline (Backend)

The backend .husky/pre-commit hook runs these steps in order:

StepAction
1npx prisma generate — regenerate the Prisma client
2yarn build — catch TypeScript / module-graph errors
3Schema sync — regenerate schema.gql if any *.resolver.ts / *.dto.ts / *.input.ts / *.entity.ts is staged (auto-stages the refreshed schema.gql)
4Dev-boot probe — SERVICE_PORT=14001 npm run start:dev, wait up to 60 s for the log line Application started successfully, then kill the server. Fails the commit if boot times out or the bootstrap logs Failed to start application
Tests are NOT in the pre-commit hook

Jest has been intentionally removed from pre-commit for faster iteration. CI runs the full npm run test:safe suite on every push to dev. Developers are expected to run npm run test:safe locally before pushing when they've touched service / resolver code.

Dev-boot step needs local infra

The boot probe requires a reachable Postgres and Redis. The default local-dev stack (aiqlick-local-postgres, aiqlick-local-redis) satisfies both — point .env DATABASE_URL and REDIS_URL at localhost so the probe doesn't hit shared Redis Cloud credentials. See Local Docker setup.

Port 14001

The probe uses SERVICE_PORT=14001 (overrides PORT and the default 4001 — see src/main.ts) so it coexists with a dev server already running on 4001.

Environment Variables for Testing

VariablePurpose
NODE_ENV=testRequired for destructive test operations
TEST_DATABASE_URLDedicated test database connection
RUN_REAL_INTEGRATION_TESTS=trueEnable real DB integration tests
RUN_REAL_E2E_TESTS=trueEnable E2E tests
E2E_API_URLTarget endpoint for E2E tests
ALLOW_DESTRUCTIVE_TESTS=trueAllow data-deleting operations
ALLOW_PRODUCTION_E2E=trueOverride production endpoint protection
warning

Never set ALLOW_PRODUCTION_E2E=true in CI pipelines. This flag exists only for manual validation by authorized engineers.