diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index b3ddd50..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,69 +0,0 @@ -name: CI - -on: - push: - branches: - - '*' - - '*/*' - -jobs: - client-code-checks: - name: Client Code Checks - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.ref }} - - - name: Set up Node - uses: actions/setup-node@v4 - with: - path: ~/.npm - key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} - node-version: '20' - restore-keys: | - ${{ runner.os }}-node- - - - name: Clean and Install - run: cd client && npm ci - - name: Build - run: cd client && npm run build - - name: Lint - run: cd client && npm run lint - #- name: Unit tests - # run: npm run test - - java-code-checks: - name: Java Code Checks - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.ref }} - - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - cache: 'maven' - - - name: Cache Maven packages - uses: actions/cache@v4 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: cd java-api && ./mvnw --no-transfer-progress clean compile -DskipTests - - - name: Google Checkstyle - run: cd java-api && ./mvnw --no-transfer-progress checkstyle:checkstyle -Dskip.checkstyle=false - - - name: All Tests - run: cd java-api && ./mvnw --no-transfer-progress clean verify -P all-tests --file pom.xml \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bd443c5..980ea8c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,8 +7,8 @@ on: workflow_dispatch: jobs: - code-checks: - name: Code Checks + client-code-checks: + name: Client Code Checks runs-on: ubuntu-latest steps: - name: Checkout @@ -27,5 +27,54 @@ jobs: run: cd client && npm run build - name: Lint run: cd client && npm run lint - #- name: Unit tests - # run: npm run test + - name: Unit tests + run: cd client && npm run test:coverage + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@v3.0.0 + with: + projectBaseDir: client + args: > + -Dsonar.organization=ricardo-campos-org + -Dsonar.projectKey=ricardo-campos-org_react-typescript-todolist + -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info + -Dsonar.typescript.tsconfigPaths=tsconfig.json + -Dsonar.sources=src/ + -Dsonar.exclusions=src/__test__/** + -Dsonar.tests=src/__test__/ + -Dsonar.verbose=false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + java-code-checks: + name: Server Code Checks + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + cache: 'maven' + + - name: Cache Maven packages + uses: actions/cache@v4 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Build with Maven + run: cd java-api && ./mvnw --no-transfer-progress clean compile -DskipTests + + - name: Google Checkstyle + run: cd java-api && ./mvnw --no-transfer-progress checkstyle:checkstyle -Dskip.checkstyle=false + + - name: All Tests + run: cd java-api && ./mvnw --no-transfer-progress clean verify -P all-tests --file pom.xml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 732930e..30e0006 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -8,8 +8,8 @@ concurrency: cancel-in-progress: true jobs: - code-checks: - name: Code Checks + client-code-checks: + name: Client Code Checks runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -23,14 +23,30 @@ jobs: run: cd client && npm run build - name: Lint run: cd client && npm run lint - #- name: Unit tests - # run: npm run test + - name: Unit tests + run: cd client && npm run test:coverage + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@v3.0.0 + with: + projectBaseDir: client + args: > + -Dsonar.organization=ricardo-campos-org + -Dsonar.projectKey=ricardo-campos-org_react-typescript-todolist + -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info + -Dsonar.typescript.tsconfigPaths=tsconfig.json + -Dsonar.sources=src/ + -Dsonar.exclusions=src/__test__/** + -Dsonar.tests=src/__test__/ + -Dsonar.verbose=false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - docker-build: - name: Build Docker image + client-docker-build: + name: Build Client Docker image runs-on: ubuntu-latest needs: - - code-checks + - client-code-checks steps: - uses: actions/checkout@v4 - uses: docker/setup-buildx-action@v3 @@ -52,3 +68,36 @@ jobs: build-args: BUILD=${{ github.event.number }} cache-from: type=gha cache-to: type=gha,mode=max + + java-code-checks: + name: Server Code Checks + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + cache: 'maven' + + - name: Cache Maven packages + uses: actions/cache@v4 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Build with Maven + run: cd java-api && ./mvnw --no-transfer-progress clean compile -DskipTests + + - name: Google Checkstyle + run: cd java-api && ./mvnw --no-transfer-progress checkstyle:checkstyle -Dskip.checkstyle=false + + - name: All Tests + run: cd java-api && ./mvnw --no-transfer-progress clean verify -P all-tests --file pom.xml diff --git a/client/src/__test__/App.test.tsx b/client/src/__test__/App.test.tsx new file mode 100644 index 0000000..b63206d --- /dev/null +++ b/client/src/__test__/App.test.tsx @@ -0,0 +1,14 @@ +import React from 'react'; +import { test } from "vitest"; +import App from "../App"; +import { render } from "@testing-library/react"; +import AuthContext from "../context/AuthContext"; +import authContextMock from "./__mocks__/authContextMock"; + +test('Renders the app', () => { + render( + + + + ); +}); diff --git a/client/src/__test__/__mocks__/authContextMock.ts b/client/src/__test__/__mocks__/authContextMock.ts new file mode 100644 index 0000000..3ba2542 --- /dev/null +++ b/client/src/__test__/__mocks__/authContextMock.ts @@ -0,0 +1,12 @@ +import { vi } from "vitest"; + +const authContextMock = { + signed: true, + user: undefined, + checkCurrentAuthUser: vi.fn(), + signIn: vi.fn(), + signOut: vi.fn(), + isAdmin: false +}; + +export default authContextMock; diff --git a/client/src/__test__/setup.ts b/client/src/__test__/setup.ts new file mode 100644 index 0000000..3be45d6 --- /dev/null +++ b/client/src/__test__/setup.ts @@ -0,0 +1,15 @@ +import { afterEach } from 'vitest'; +import { cleanup } from '@testing-library/react'; + +// runs a cleanup after each test case (e.g. clearing jsdom) +afterEach(() => { + cleanup(); +}); + +window.matchMedia = window.matchMedia || function() { + return { + matches: false, + addListener: function() {}, + removeListener: function() {} + }; +}; diff --git a/client/tsconfig.json b/client/tsconfig.json index 50daaae..ac1c0ab 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -25,5 +25,8 @@ }, "include": [ "src" + ], + "exclude": [ + "src/__test__/" ] }