Skip to main content

Test Automation

All tests run automatically in CI/CD pipelines. This ensures code quality and prevents regressions.

CI/CD Pipeline

GitHub Actions Workflow

Tests run automatically on:

  • Every commit - Quick feedback
  • Pull requests - Full test suite
  • Before deployment - Final verification

Test Stages

  1. Lint & Format - Code quality checks
  2. Unit Tests - Fast feedback
  3. Integration Tests - API and database
  4. E2E Tests - Complete workflows
  5. Security Tests - Vulnerability scanning

GitHub Actions Configuration

# .github/workflows/test.yml
name: Tests

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: npm ci

- name: Run linter
run: npm run lint

- name: Run unit tests
run: npm run test:unit

- name: Run integration tests
run: npm run test:integration
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test

- name: Run E2E tests
run: npm run test:e2e

- name: Security audit
run: npm audit --audit-level=high

- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage/lcov.info

Pre-commit Hooks

Husky Configuration

// package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm run test:unit"
}
},
"lint-staged": {
"*.{js,ts}": [
"eslint --fix",
"prettier --write"
]
}
}

What Runs Pre-commit

  • Linter - Code style checks
  • Formatter - Auto-format code
  • Unit tests - Quick validation
  • Security scan - Check for vulnerabilities

Test Reports

Coverage Reports

Coverage reports generated automatically:

  • Local: coverage/lcov-report/index.html
  • CI: Uploaded to Codecov
  • PR Comments: Coverage diff in PR

Test Results

Test results available:

  • GitHub Actions - View in Actions tab
  • Playwright Report - HTML report generated
  • Jest Report - Console and JSON output

Monitoring Test Health

Metrics to Track

  • Test execution time - Should be fast
  • Test pass rate - Should be 100%
  • Coverage percentage - Should be 80%+
  • Flaky tests - Should be zero

Alerts

Set up alerts for:

  • Test failures in main branch
  • Coverage drops below threshold
  • Tests taking too long
  • Security vulnerabilities found

Best Practices

  1. Keep tests fast - Unit tests < 1s, Integration < 10s
  2. Fix flaky tests immediately - Don't ignore failures
  3. Update tests with code changes - Tests should reflect reality
  4. Review test failures - Don't just rerun
  5. Monitor test metrics - Track trends over time

Resources