name: Test Coverage
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests with coverage
run: npm run test:coverage
- name: Check coverage thresholds
run: |
# Verify that coverage meets thresholds
COVERAGE=$(cat coverage/coverage-final.json | grep -o '"statements":{"total":[0-9]*,"covered":[0-9]*,"pct":[0-9.]*' | grep -o 'pct":[0-9.]*' | grep -o '[0-9.]*' | head -n 1)
BRANCH_COVERAGE=$(cat coverage/coverage-final.json | grep -o '"branches":{"total":[0-9]*,"covered":[0-9]*,"pct":[0-9.]*' | grep -o 'pct":[0-9.]*' | grep -o '[0-9.]*' | head -n 1)
FUNCTION_COVERAGE=$(cat coverage/coverage-final.json | grep -o '"functions":{"total":[0-9]*,"covered":[0-9]*,"pct":[0-9.]*' | grep -o 'pct":[0-9.]*' | grep -o '[0-9.]*' | head -n 1)
echo "Statement coverage: $COVERAGE%"
echo "Branch coverage: $BRANCH_COVERAGE%"
echo "Function coverage: $FUNCTION_COVERAGE%"
# Check against thresholds
STATEMENT_THRESHOLD=70
BRANCH_THRESHOLD=60
FUNCTION_THRESHOLD=70
if (( $(echo "$COVERAGE < $STATEMENT_THRESHOLD" | bc -l) )); then
echo "Error: Statement coverage ($COVERAGE%) is below threshold ($STATEMENT_THRESHOLD%)"
exit 1
fi
if (( $(echo "$BRANCH_COVERAGE < $BRANCH_THRESHOLD" | bc -l) )); then
echo "Error: Branch coverage ($BRANCH_COVERAGE%) is below threshold ($BRANCH_THRESHOLD%)"
exit 1
fi
if (( $(echo "$FUNCTION_COVERAGE < $FUNCTION_THRESHOLD" | bc -l) )); then
echo "Error: Function coverage ($FUNCTION_COVERAGE%) is below threshold ($FUNCTION_THRESHOLD%)"
exit 1
fi
echo "✅ All coverage thresholds passed!"