diff --git a/.github/workflows/workflow_backend.yml b/.github/workflows/workflow_backend.yml index 36b49fa..f22ddec 100644 --- a/.github/workflows/workflow_backend.yml +++ b/.github/workflows/workflow_backend.yml @@ -2,32 +2,33 @@ name: Backend CI on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: build: - runs-on: ubuntu-latest defaults: run: working-directory: ./backend steps: - - uses: actions/checkout@v2 - with: - path: 'backend' - ref: main + - uses: actions/checkout@v2 + with: + path: "backend" + ref: main - - name: Use Node.js - uses: actions/setup-node@v2 - with: - node-version: '20.x' + - name: Use Node.js + uses: actions/setup-node@v2 + with: + node-version: "20.x" - - name: Install Dependencies - run: npm install + - name: Install Dependencies + run: npm install - - name: Run ESLint - run: npm run format + - name: Run ESLint + run: npm run format + - name: Run Tests + run: npm run test diff --git a/.github/workflows/workflow_frontend.yml b/.github/workflows/workflow_frontend.yml index 6b2ec52..dad22b9 100644 --- a/.github/workflows/workflow_frontend.yml +++ b/.github/workflows/workflow_frontend.yml @@ -33,4 +33,10 @@ jobs: run: npm install vite - name: Run ESLint - run: npm run format \ No newline at end of file + run: npm run format + + - name: Run Tests + run: npm run test + + - name: Build + run: npm run build \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index d431fd6..fd28760 100644 --- a/backend/package.json +++ b/backend/package.json @@ -7,7 +7,7 @@ "scripts": { "start": "node -r dotenv/config --experimental-json-modules index.js", "dev": "nodemon -r dotenv/config --experimental-json-modules index.js", - "test": "jest", + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", "build": "npm i", "format": "cd .. && npm run format" }, diff --git a/backend/test/models/category.model.test.js b/backend/test/models/category.model.test.js deleted file mode 100644 index b992044..0000000 --- a/backend/test/models/category.model.test.js +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-env jest */ - -import mongoose from "mongoose" -import Category from "../../models/category.model.js" - -describe("Category Model Test", () => { - beforeAll(async () => { - const url = "mongodb://127.0.0.1/testDB" - await mongoose.connect(url, { useNewUrlParser: true }) - }) - - afterAll(async () => { - await mongoose.connection.close() - }) - - it("create & save category successfully", async () => { - const categoryData = { - name: "Test Category", - description: "Test Description", - courses: [], - } - const validCategory = new Category(categoryData) - const savedCategory = await validCategory.save() - - // Object Id should be defined when successfully saved to MongoDB. - expect(savedCategory._id).toBeDefined() - expect(savedCategory.name).toBe(categoryData.name) - expect(savedCategory.description).toBe(categoryData.description) - expect(savedCategory.courses).toStrictEqual(categoryData.courses) - }) -}) diff --git a/backend/test/utils/secToDuration.test.js b/backend/test/utils/secToDuration.test.js new file mode 100644 index 0000000..48c097d --- /dev/null +++ b/backend/test/utils/secToDuration.test.js @@ -0,0 +1,15 @@ +import convertSecondsToDuration from "../../utils/secToDuration"; + +describe("convertSecondsToDuration", () => { + it("should correctly convert seconds to hours and minutes", () => { + expect(convertSecondsToDuration(3661)).toBe("1h 1m"); + }); + + it("should correctly convert seconds to minutes and seconds", () => { + expect(convertSecondsToDuration(61)).toBe("1m 1s"); + }); + + it("should correctly convert seconds to seconds", () => { + expect(convertSecondsToDuration(1)).toBe("1s"); + }); +}); diff --git a/frontend/package.json b/frontend/package.json index 7729a94..b3ab206 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -8,7 +8,7 @@ "build": "vite build", "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", - "test": "vitest", + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", "format": "cd .. && npm run format" }, "dependencies": { @@ -48,4 +48,4 @@ "vite": "^4.4.5", "vitest": "^0.34.6" } -} +} \ No newline at end of file diff --git a/frontend/src/test/utils/avgRating.test.js b/frontend/src/test/utils/avgRating.test.js new file mode 100644 index 0000000..22f79f6 --- /dev/null +++ b/frontend/src/test/utils/avgRating.test.js @@ -0,0 +1,23 @@ +import GetAvgRating from "../../utils/avgRating"; + +describe("GetAvgRating", () => { + it("should return 0 when the array is empty", () => { + expect(GetAvgRating([])).toBe(0); + }); + + it("should correctly calculate the average rating", () => { + const ratings = [ + { rating: 5 }, + { rating: 4 }, + { rating: 3 }, + { rating: 2 }, + { rating: 1 }, + ]; + expect(GetAvgRating(ratings)).toBe(3); + }); + + it("should correctly round the average rating to one decimal place", () => { + const ratings = [{ rating: 5 }, { rating: 4 }, { rating: 4 }]; + expect(GetAvgRating(ratings)).toBe(4.3); + }); +});