Skip to content
Open
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
69 changes: 69 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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/



4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ npm run dev
- [ ] add validation
- [ ] improve error handling
- [ ] complete the tests
- [ ] docker for production
- [ ] docker for production

comentario
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ export default {
testEnvironment: "node",
coveragePathIgnorePatterns: ["/node_modules/"],
transform: {},

coverageThreshold: {
global: {
branches: 5,
functions: 5,
lines: 5,
statements: 5
}
}

};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
25 changes: 15 additions & 10 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});