From 20c08e1bb5a788f582f67b22d8cc44a919a83a10 Mon Sep 17 00:00:00 2001 From: Saidev Dhal Date: Mon, 25 Nov 2024 09:09:30 +0530 Subject: [PATCH] fix workflows --- .github/workflows/deploy-triggers.yml | 2 +- .github/workflows/format-and-push.yml | 6 --- ...nt-and-test.yml => lint-and-typecheck.yml} | 37 ++++++--------- .../workflows/test-multiple-node-versions.yml | 47 +++++++++---------- .../hello.test.ts => __tests__/db.test.ts} | 23 ++------- apps/api/__tests__/health.test.ts | 18 +++++++ package.json | 1 + 7 files changed, 61 insertions(+), 73 deletions(-) rename .github/workflows/{lint-and-test.yml => lint-and-typecheck.yml} (60%) rename apps/api/{tests/hello.test.ts => __tests__/db.test.ts} (73%) create mode 100644 apps/api/__tests__/health.test.ts diff --git a/.github/workflows/deploy-triggers.yml b/.github/workflows/deploy-triggers.yml index f6e4621..d0ee922 100644 --- a/.github/workflows/deploy-triggers.yml +++ b/.github/workflows/deploy-triggers.yml @@ -29,4 +29,4 @@ jobs: env: TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }} run: | - npx trigger.dev@latest deploy + pnpm dlx trigger.dev@latest deploy diff --git a/.github/workflows/format-and-push.yml b/.github/workflows/format-and-push.yml index 92c9cbd..1ff8a93 100644 --- a/.github/workflows/format-and-push.yml +++ b/.github/workflows/format-and-push.yml @@ -4,8 +4,6 @@ on: push: branches: - main - - 'feat/*' - - 'bugfix/*' pull_request: branches: - main @@ -52,10 +50,6 @@ jobs: echo "formatting_required=true" >> $GITHUB_ENV fi - - name: Format code with Prettier if needed - if: env.formatting_required == 'true' - run: pnpm format:write - - name: Check for changes and push if needed run: | if [ -n "$(git status --porcelain)" ]; then diff --git a/.github/workflows/lint-and-test.yml b/.github/workflows/lint-and-typecheck.yml similarity index 60% rename from .github/workflows/lint-and-test.yml rename to .github/workflows/lint-and-typecheck.yml index 60ad123..602f254 100644 --- a/.github/workflows/lint-and-test.yml +++ b/.github/workflows/lint-and-typecheck.yml @@ -1,11 +1,9 @@ -name: Lint and Test +name: Lint and Type Check on: push: branches: - main - - 'feat/*' - - 'bugfix/*' pull_request: branches: - main @@ -27,6 +25,9 @@ jobs: - name: Install pnpm run: npm install -g pnpm + - name: Install turbo + run: pnpm install -g turbo + - name: Cache pnpm modules uses: actions/cache@v3 with: @@ -36,7 +37,7 @@ jobs: ${{ runner.os }}-pnpm- - name: Install dependencies - run: pnpm install --shamefully-hoist + run: pnpm install lint: runs-on: ubuntu-latest @@ -45,39 +46,31 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Node.js (again to ensure pnpm is available) + - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '21.7.1' - - name: Install pnpm (again in case the previous job isolated the environment) - run: npm install -g pnpm - - - name: Install Turbo CLI - run: npm install -g turbo + - name: Use pnpm & turbo from setup + run: pnpm --version && turbo --version - name: Run Lint run: pnpm lint - test: + type-check: runs-on: ubuntu-latest needs: [setup, lint] - env: - DATABASE_URL: ${{ secrets.DATABASE_URL }} - RESEND_API: ${{ secrets.RESEND_API }} steps: - name: Checkout code uses: actions/checkout@v2 - - name: Set up .env file for tests - run: | - echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> apps/api/.env - echo "RESEND_API=${{ secrets.RESEND_API }}" >> apps/api/.env - - - name: Set up Node.js (again to ensure pnpm is available) + - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '21.7.1' - - name: Run Tests - run: pnpm test + - name: Use turbo from setup + run: turbo --version + + - name: Run Type Check + run: turbo typecheck diff --git a/.github/workflows/test-multiple-node-versions.yml b/.github/workflows/test-multiple-node-versions.yml index d3771c6..fc7a72d 100644 --- a/.github/workflows/test-multiple-node-versions.yml +++ b/.github/workflows/test-multiple-node-versions.yml @@ -4,8 +4,6 @@ on: push: branches: - main - - 'feat/*' - - 'bugfix/*' pull_request: branches: - main @@ -15,35 +13,37 @@ on: jobs: test: runs-on: ubuntu-latest - env: - DATABASE_URL: ${{ secrets.DATABASE_URL }} - RESEND_API: ${{ secrets.RESEND_API }} strategy: matrix: - node-version: [20.18.0, 18.20.4, 22.11.0, 19.9.0, 23.1.0] # Ensure all versions are >= v18 + node-version: [20.18.0, 18.20.4, 22.11.0, 19.9.0, 23.1.0] # Ensure all versions are >= v18 steps: - name: Checkout code uses: actions/checkout@v2 - - name: Set up .env file for tests in apps/api - run: | - echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> apps/api/.env - echo "RESEND_API=${{ secrets.RESEND_API }}" >> apps/api/.env - - - name: Set environment variables for vitest in apps/api - run: | - echo "RESEND_API=${{ secrets.RESEND_API }}" >> apps/api/.env - echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> apps/api/.env - cat apps/api/.env # Add this line to log and verify env variables - - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - - name: Install pnpm - run: npm install -g pnpm + - name: Set environment variables + run: | + echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> $GITHUB_ENV + echo "RESEND_API=${{ secrets.RESEND_API }}" >> $GITHUB_ENV + + - name: Log environment variables (debug) + run: | + echo "DATABASE_URL=$DATABASE_URL" + echo "RESEND_API=$RESEND_API" + + - name: Set up .env file for tests in apps/api + run: | + echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> apps/api/.env + echo "RESEND_API=${{ secrets.RESEND_API }}" >> apps/api/.env + cat apps/api/.env # Verify .env contents + + - name: Install pnpm & turbo + run: npm install -g pnpm && pnpm install -g turbo - name: Cache pnpm modules uses: actions/cache@v3 @@ -56,10 +56,5 @@ jobs: - name: Install dependencies run: pnpm install - - name: Log environment variables for verification (debug) - run: | - echo "DATABASE_URL=$DATABASE_URL" - echo "RESEND_API=$RESEND_API" - - - name: Run tests - run: pnpm test + - name: Run Turbo tests + run: turbo test diff --git a/apps/api/tests/hello.test.ts b/apps/api/__tests__/db.test.ts similarity index 73% rename from apps/api/tests/hello.test.ts rename to apps/api/__tests__/db.test.ts index 89d267f..770a9d3 100644 --- a/apps/api/tests/hello.test.ts +++ b/apps/api/__tests__/db.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; -import { GET, POST, PATCH, DELETE } from "../app/api/[[...route]]/route"; import { prisma } from "@repo/db"; +import { DELETE, GET, PATCH, POST } from "@/app/api/[[...route]]/route"; vi.mock("@repo/db", () => ({ prisma: { @@ -18,7 +18,7 @@ describe("API Routes", () => { vi.clearAllMocks(); }); - it("POST http:/localhost:3000/api/hello should create a new user", async () => { + it("POST /api/hello should create a new user", async () => { // Mock the response of prisma.user.create const newUser = { id: "123", name: "New User" }; prisma.user.create.mockResolvedValue(newUser); @@ -34,7 +34,7 @@ describe("API Routes", () => { expect(json.test).toEqual(newUser); }); - it("GET http:/localhost:3000/api/hello should return users", async () => { + it("GET /api/hello should return users", async () => { // Mock the response of prisma.user.findMany const mockUsers = [{ id: "123", name: "New User" }]; prisma.user.findMany.mockResolvedValue(mockUsers); @@ -49,7 +49,7 @@ describe("API Routes", () => { expect(json.test).toEqual(mockUsers); }); - it("PATCH http:/localhost:3000/api/hello should update the user name", async () => { + it("PATCH /api/hello should update the user name", async () => { // Mock the response of prisma.user.update const updatedUser = { id: "123", name: "Updated User" }; prisma.user.update.mockResolvedValue(updatedUser); @@ -65,7 +65,7 @@ describe("API Routes", () => { expect(json.test).toEqual(updatedUser); }); - it("DELETE http:/localhost:3000/api/hello should delete the user", async () => { + it("DELETE /api/hello should delete the user", async () => { // Mock the response of prisma.user.delete const deletedUser = { id: "2", name: "Deleted User" }; prisma.user.delete.mockResolvedValue(deletedUser); @@ -80,17 +80,4 @@ describe("API Routes", () => { expect(json.test).toEqual(deletedUser); }); - it("GET /api/health should return health status", async () => { - const req = new Request("http:/localhost:3000/api/health", { - method: "GET", - }); - const res = await GET(req); - - expect(res.status).toBe(200); - const json = await res.json(); - expect(json).toEqual({ - message: "i am alive", - status: 200, - }); - }); }); diff --git a/apps/api/__tests__/health.test.ts b/apps/api/__tests__/health.test.ts new file mode 100644 index 0000000..3ff220f --- /dev/null +++ b/apps/api/__tests__/health.test.ts @@ -0,0 +1,18 @@ +import { GET } from "@/app/api/[[...route]]/route"; +import { describe, it, expect } from "vitest"; + +describe("Health API", () => { + it("GET /api/health should return health status", async () => { + const req = new Request("http://localhost:3000/api/health", { + method: "GET", + }); + const res = await GET(req); + + expect(res.status).toBe(200); + const json = await res.json(); + expect(json).toEqual({ + message: "i am alive", + status: 200, + }); + }); +}); diff --git a/package.json b/package.json index 52be98f..c078c46 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "lint": "turbo run lint --force", "lint:fix": "turbo run lint:fix", "test": "turbo run test --force", + "typecheck": "turbo run typecheck", "format:write": "turbo run format:write", "format:check": "turbo run format:check" },