migration to typescript

This commit is contained in:
JakkrapartXD 2026-01-09 06:28:15 +00:00
parent 924000b084
commit 9fde77468a
41 changed files with 11952 additions and 10164 deletions

View file

@ -4,7 +4,7 @@ description: How to test backend APIs and services
# Testing Workflow
Follow these steps to write and run tests for the E-Learning Platform backend.
Follow these steps to write and run tests for the E-Learning Platform backend using TypeScript and Jest.
---
@ -22,10 +22,12 @@ tests/
## Step 1: Setup Test Environment
Create `tests/setup.js`:
Create `tests/setup.ts`:
```typescript
import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcrypt';
```javascript
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
beforeAll(async () => {
@ -44,8 +46,6 @@ afterAll(async () => {
async function createTestUsers() {
// Create admin, instructor, student
const bcrypt = require('bcrypt');
await prisma.user.createMany({
data: [
{
@ -75,11 +75,12 @@ async function createTestUsers() {
## Step 2: Write Unit Tests
Create `tests/unit/course.service.test.js`:
Create `tests/unit/course.service.test.ts`:
```typescript
import { courseService } from '../../src/services/course.service';
import { PrismaClient } from '@prisma/client';
```javascript
const courseService = require('../../src/services/course.service');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
describe('Course Service', () => {
@ -93,7 +94,7 @@ describe('Course Service', () => {
is_free: false
};
const course = await courseService.createCourse(courseData, 1);
const course = await courseService.create(courseData, 1);
expect(course).toHaveProperty('id');
expect(course.status).toBe('DRAFT');
@ -108,7 +109,7 @@ describe('Course Service', () => {
};
await expect(
courseService.createCourse(courseData, 1)
courseService.create(courseData, 1)
).rejects.toThrow();
});
});
@ -134,14 +135,16 @@ describe('Course Service', () => {
## Step 3: Write Integration Tests
Create `tests/integration/courses.test.js`:
Create `tests/integration/courses.test.ts`:
```javascript
const request = require('supertest');
const app = require('../../src/app');
```typescript
import request from 'supertest';
import app from '../../src/app';
describe('Course API', () => {
let adminToken, instructorToken, studentToken;
let adminToken: string;
let instructorToken: string;
let studentToken: string;
beforeAll(async () => {
// Login as different users
@ -217,7 +220,7 @@ describe('Course API', () => {
.query({ category: 1 });
expect(response.status).toBe(200);
response.body.data.forEach(course => {
response.body.data.forEach((course: any) => {
expect(course.category_id).toBe(1);
});
});
@ -229,15 +232,15 @@ describe('Course API', () => {
## Step 4: Test File Uploads
Create `tests/integration/file-upload.test.js`:
Create `tests/integration/file-upload.test.ts`:
```javascript
const request = require('supertest');
const app = require('../../src/app');
const path = require('path');
```typescript
import request from 'supertest';
import app from '../../src/app';
import path from 'path';
describe('File Upload API', () => {
let instructorToken;
let instructorToken: string;
beforeAll(async () => {
const res = await request(app)
@ -330,10 +333,12 @@ Update `package.json`:
"test:coverage": "jest --coverage"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"coveragePathIgnorePatterns": ["/node_modules/"],
"setupFilesAfterEnv": ["<rootDir>/tests/setup.js"],
"testMatch": ["**/tests/**/*.test.js"]
"setupFilesAfterEnv": ["<rootDir>/tests/setup.ts"],
"testMatch": ["**/tests/**/*.test.ts"],
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
}
```