Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N11 - unit tests #65

Merged
merged 30 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}
tituschewxj marked this conversation as resolved.
Show resolved Hide resolved
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
Loading