Skip to content

Commit dd425b4

Browse files
feat: Add comprehensive test suite for curriculum and progress services (#646)
- Add unit tests for curriculum service with >90% coverage - Add unit tests for progress service with >90% coverage - Add integration tests covering full API workflows - Create comprehensive testing documentation - Add jest setup configuration - Add GitHub Actions CI workflow for automated testing - Fix duplicate test script in package.json Tests cover: - Cache hit/miss scenarios - Database fallback resilience - Difficulty filtering - Progress tracking and lesson completion - Percentage calculations - Student isolation - Authentication and authorization - End-to-end user journeys Educational comments included throughout for learning purposes. Closes #609
1 parent 2f792a3 commit dd425b4

7 files changed

Lines changed: 2219 additions & 4 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: Backend Tests
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
paths:
7+
- 'backend/**'
8+
- '.github/workflows/backend-tests.yml'
9+
pull_request:
10+
branches: [ main, develop ]
11+
paths:
12+
- 'backend/**'
13+
14+
jobs:
15+
test:
16+
runs-on: ubuntu-latest
17+
18+
strategy:
19+
matrix:
20+
node-version: [18.x, 20.x]
21+
22+
services:
23+
postgres:
24+
image: postgres:15
25+
env:
26+
POSTGRES_USER: test
27+
POSTGRES_PASSWORD: test
28+
POSTGRES_DB: test_db
29+
options: >-
30+
--health-cmd pg_isready
31+
--health-interval 10s
32+
--health-timeout 5s
33+
--health-retries 5
34+
ports:
35+
- 5432:5432
36+
37+
redis:
38+
image: redis:7
39+
options: >-
40+
--health-cmd "redis-cli ping"
41+
--health-interval 10s
42+
--health-timeout 5s
43+
--health-retries 5
44+
ports:
45+
- 6379:6379
46+
47+
steps:
48+
- name: Checkout code
49+
uses: actions/checkout@v3
50+
51+
- name: Setup Node.js ${{ matrix.node-version }}
52+
uses: actions/setup-node@v3
53+
with:
54+
node-version: ${{ matrix.node-version }}
55+
cache: 'npm'
56+
cache-dependency-path: backend/package-lock.json
57+
58+
- name: Install dependencies
59+
working-directory: ./backend
60+
run: npm ci
61+
62+
- name: Run Prisma generate
63+
working-directory: ./backend
64+
env:
65+
DATABASE_URL: postgresql://test:test@localhost:5432/test_db
66+
run: npx prisma generate
67+
68+
- name: Run database migrations
69+
working-directory: ./backend
70+
env:
71+
DATABASE_URL: postgresql://test:test@localhost:5432/test_db
72+
run: npx prisma migrate deploy || true
73+
74+
- name: Run tests with coverage
75+
working-directory: ./backend
76+
env:
77+
NODE_ENV: test
78+
DATABASE_URL: postgresql://test:test@localhost:5432/test_db
79+
REDIS_URL: redis://localhost:6379
80+
JWT_SECRET: test-jwt-secret
81+
run: npm run test:coverage
82+
83+
- name: Check coverage threshold
84+
working-directory: ./backend
85+
run: |
86+
coverage=$(cat coverage/coverage-summary.json | jq '.total.lines.pct')
87+
echo "Coverage: $coverage%"
88+
if (( $(echo "$coverage < 90" | bc -l) )); then
89+
echo "❌ Coverage $coverage% is below 90% threshold"
90+
exit 1
91+
fi
92+
echo "✅ Coverage $coverage% meets the 90% threshold"
93+
94+
- name: Upload coverage to Codecov
95+
uses: codecov/codecov-action@v3
96+
with:
97+
files: ./backend/coverage/lcov.info
98+
flags: backend
99+
name: backend-coverage
100+
fail_ci_if_error: false
101+
102+
- name: Comment PR with coverage
103+
if: github.event_name == 'pull_request'
104+
uses: romeovs/lcov-reporter-action@v0.3.1
105+
with:
106+
lcov-file: ./backend/coverage/lcov.info
107+
github-token: ${{ secrets.GITHUB_TOKEN }}
108+
delete-old-comments: true

backend/jest.setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest setup file for global test configuration
2+
process.env.NODE_ENV = 'test';
3+
process.env.DATABASE_URL = 'postgres://dummy:dummy@localhost:5432/dummy';
4+
process.env.JWT_SECRET = 'test-secret-key';
5+
process.env.REDIS_URL = 'redis://localhost:6379';

backend/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
"start": "node dist/src/index.js",
99
"start:prod": "node dist/src/index.js",
1010
"dev": "tsx watch src/index.ts",
11-
"test": "jest --runInBand",
12-
"test:coverage": "jest --coverage --runInBand",
13-
"collaboration": "tsx src/collaborationServer.ts",
14-
"test": "DATABASE_URL='postgres://dummy:dummy@localhost:5432/dummy' node --experimental-vm-modules node_modules/jest/bin/jest.js"
11+
"test": "NODE_ENV=test jest --runInBand",
12+
"test:coverage": "NODE_ENV=test jest --coverage --runInBand",
1513
"collaboration": "tsx src/collaborationServer.ts"
1614
},
1715
"keywords": [],

0 commit comments

Comments
 (0)