diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ab5ce4b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,69 @@ +name: CI - Node.js MySQL API + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [18, 20] + + + + services: + mysql: + image: mysql:8.0 + env: + MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }} + MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }} + MYSQL_USER: ${{ secrets.MYSQL_USER }} + MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }} + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -h localhost -u fazt -pfaztpassword" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Use Node.js + uses: actions/setup-node@v4 + with: + node-version: 18 + cache: npm + + - name: Install dependencies + run: npm install + + - name: Wait for MySQL to be ready + run: | + echo "Waiting for MySQL..." + until mysql -h 127.0.0.1 -u fazt -pfaztpassword -e "SELECT 1"; do + sleep 3 + done + + - name: Initialize database + run: | + mysql -h 127.0.0.1 -u fazt -pfaztpassword companydb < db/database.sql + + - name: Run tests with coverage + run: npm run test:coverage + + - name: Upload coverage artifact + uses: actions/upload-artifact@v4 + with: + name: coverage-report + path: coverage/ + + + diff --git a/README.md b/README.md index ff49045..efe3120 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,6 @@ npm run dev - [ ] add validation - [ ] improve error handling - [ ] complete the tests -- [ ] docker for production \ No newline at end of file +- [ ] docker for production + +comentario \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index 59f24eb..7933b58 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,4 +4,14 @@ export default { testEnvironment: "node", coveragePathIgnorePatterns: ["/node_modules/"], transform: {}, + + coverageThreshold: { + global: { + branches: 5, + functions: 5, + lines: 5, + statements: 5 + } + } + }; diff --git a/package.json b/package.json index c7cf8db..9634ec8 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,12 @@ "scripts": { "dev": "nodemon src/index.js", "start": "node src/index.js", + "pretest": "cross-env NODE_ENV=test jest --clearCache", + + "lint": "eslint .", + "test": "NODE_OPTIONS=--experimental-vm-modules jest --coverage", - "pretest": "cross-env NODE_ENV=test jest --clearCache" + "test:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --coverage --ci" }, "keywords": [], "author": "", diff --git a/tests/index.test.js b/tests/index.test.js index bfef279..8ddbaf9 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,16 +1,21 @@ -import app from "../src/app"; -import request from "supertest"; +import app from '../src/app'; +import request from 'supertest'; +import { pool } from '../src/db'; -describe("Index Routes", () => { - it("should respond welcome", async () => { - const res = await request(app).get("/"); +describe('Index Routes', () => { + it('should respond welcome', async () => { + const res = await request(app).get('/'); expect(res.statusCode).toEqual(200); - expect(res.body).toEqual({ message: "welcome to my api" }); + expect(res.body).toEqual({ message: 'welcome to my api' }); }); - it("should respond pong", async () => { - const res = await request(app).get("/ping"); + it('should respond pong', async () => { + const res = await request(app).get('/ping'); expect(res.statusCode).toEqual(200); - expect(res.body).toEqual({ result: "pong" }); + expect(res.body).toEqual({ result: 'pong' }); }); -}); + + afterAll(async () => { + await pool.end(); + }); +}); \ No newline at end of file