Skip to content

Commit

Permalink
Merge pull request #65 from CS3219-AY2425S1/n11-unit-tests
Browse files Browse the repository at this point in the history
N11 - unit tests
  • Loading branch information
chiaryan authored Nov 4, 2024
2 parents 0427229 + 8dd1799 commit 0d2a3bc
Show file tree
Hide file tree
Showing 9 changed files with 3,778 additions and 269 deletions.
74 changes: 73 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,81 @@ on:
branches:
- main
- staging
workflow_dispatch:

jobs:
test:
question-service-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up .env
env:
QUESTION_FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: |
cd ./apps/question-service
echo "FIREBASE_CREDENTIAL_PATH=$QUESTION_FIREBASE_CREDENTIAL_PATH" >> .env
echo "JWT_SECRET=$JWT_SECRET" >> .env
- name: Set up credentials
env:
QUESTION_FIREBASE_JSON: ${{ secrets.QUESTION_SERVICE_FIREBASE_CREDENTIAL }}
QUESTION_FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
run: |
cd ./apps/question-service
echo "$QUESTION_FIREBASE_JSON" > "./$QUESTION_FIREBASE_CREDENTIAL_PATH"
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23.x'

- name: Install Go dependencies
run: |
cd ./apps/question-service
go mod tidy
- name: Install firebase tools
run: curl -sL firebase.tools | bash

- name: Run Go tests with Firebase emulator
run: firebase emulators:exec --only firestore 'cd ./apps/question-service; go test ./...'

frontend-unit-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup .env
run: |
cd ./apps/frontend
cp .env.example .env
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '22'

- name: Install pnpm
run: npm i -g pnpm

- name: Install dependencies
run: |
cd ./apps/frontend
pnpm i
- name: Run tests
run: |
cd ./apps/frontend
pnpm test
test-docker-compose:
runs-on: ubuntu-latest

steps:
Expand Down
9 changes: 9 additions & 0 deletions apps/frontend/__tests__/Datetime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { formatTime } from "@/utils/DateTime"

describe("datetime module", () => {
it("formats a time correctly", () => {
expect(formatTime(10)).toBe("00:10")
});
})


34 changes: 34 additions & 0 deletions apps/frontend/__tests__/dependencymocking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { GetQuestions } from "@/app/services/question"
import { getToken } from "@/app/services/login-store";

const TOKEN = 'mocktoken';

jest.mock("@/app/services/login-store", () => {
return {
__esModule: true,
getToken: jest.fn(() => TOKEN)
};
})

beforeEach(() => {
global.fetch = jest.fn().mockResolvedValue({
async json() {
return {}
}
});
})

describe("mock", () => {

it("mocks correctly", async () => {
await GetQuestions()
expect(jest.mocked(getToken).mock.calls).toEqual([[]])
expect(jest.mocked(fetch).mock.calls[0][1]).toEqual({
"headers": {
"Authorization": `Bearer ${TOKEN}`,
},
"method": "GET",
})
});

})
Loading

0 comments on commit 0d2a3bc

Please sign in to comment.