Skip to content

Commit 461b18d

Browse files
authored
Merge pull request #4 from davidsilva/feature/ci-testing
Test if tests are run on pull request and push
2 parents 7defa09 + 16a65fe commit 461b18d

File tree

6 files changed

+98
-9
lines changed

6 files changed

+98
-9
lines changed

.github/workflows/ci.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- development
8+
- stage
9+
- production
10+
pull_request:
11+
branches:
12+
- main
13+
- development
14+
- stage
15+
- production
16+
17+
jobs:
18+
test:
19+
runs-on: ubuntu-latest
20+
21+
services:
22+
postgres:
23+
image: postgres:latest
24+
env:
25+
POSTGRES_USER: postgres
26+
POSTGRES_PASSWORD: postgres
27+
POSTGRES_DB: postgres
28+
ports:
29+
- 5432:5432
30+
options: >-
31+
--health-cmd "pg_isready -U postgres"
32+
--health-interval 10s
33+
--health-timeout 5s
34+
--health-retries 5
35+
36+
env:
37+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
38+
PGPASSFILE: /home/runner/.pgpass
39+
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v2
43+
44+
- name: Set up Node.js
45+
uses: actions/setup-node@v2
46+
with:
47+
node-version: '22'
48+
49+
- name: Create .pgpass file
50+
run: |
51+
echo "localhost:5432:postgres:postgres:postgres" > ~/.pgpass &&
52+
echo "localhost:5432:testdb:postgres:postgres:postgres" >> ~/.pgpass &&
53+
chmod 0600 ~/.pgpass
54+
55+
- name: Create test database
56+
run: |
57+
sudo apt-get install -y postgresql-client &&
58+
psql -h localhost -U postgres -c "CREATE DATABASE testdb;"
59+
60+
- name: Install dependencies for backend
61+
working-directory: ./backend
62+
run: npm install
63+
64+
- name: Run backend tests
65+
working-directory: ./backend
66+
run: npm test
67+
68+
- name: Install dependencies for frontend
69+
working-directory: ./frontend
70+
run: npm install
71+
72+
- name: Run frontend tests
73+
working-directory: ./frontend
74+
run: npm test
75+

backend/seeds/products_seed.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ export async function seed(knex: Knex): Promise<void> {
88
// Deletes ALL existing entries
99
await knex('products').del();
1010

11-
const products: Product[] = [
11+
await knex.raw('ALTER SEQUENCE products_id_seq RESTART WITH 1');
12+
13+
const products: Omit<Product, "id">[] = [
1214
{
13-
id: 1,
1415
name: 'Product 1',
1516
description: 'product 1 description',
1617
price: 100,
1718
status: ProductStatus.AVAILABLE,
1819
},
1920
{
20-
id: 2,
2121
name: 'Product 2',
2222
description: 'product 2 description',
2323
price: 200,
2424
status: ProductStatus.AVAILABLE,
2525
},
2626
{
27-
id: 3,
2827
name: 'Product 3',
2928
description: 'product 3 description',
3029
price: 300,

backend/seeds/users_seed.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@ export async function seed(knex: Knex): Promise<void> {
55
// Deletes ALL existing entries
66
await knex('users').del();
77

8-
const users: User[] = [
8+
await knex.raw('ALTER SEQUENCE users_id_seq RESTART WITH 1');
9+
10+
const users: Omit<User, "id">[] = [
911
{
10-
id: 1,
1112
email: 'user1@xyz.com',
1213
first_name: 'User',
1314
last_name: 'One',
1415
status: UserStatus.ACTIVE,
1516
},
1617
{
17-
id: 2,
1818
email: 'user2@xyz.com',
1919
first_name: 'User',
2020
last_name: 'Two',
2121
status: UserStatus.ACTIVE,
2222
},
2323
{
24-
id: 3,
2524
email: 'user3@xyz.com',
2625
first_name: 'User',
2726
last_name: 'Three',

backend/src/__tests__/integration/products.integration.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ afterAll(async () => {
2626
await db.destroy();
2727
});
2828

29+
beforeEach(async () => {
30+
await db.raw('BEGIN')
31+
});
32+
33+
afterEach(async () => {
34+
await db.raw('ROLLBACK')
35+
});
36+
2937
describe('GET /products', () => {
3038
it('should return a list of products', async () => {
3139
const response = await request(app).get('/products');

backend/src/__tests__/integration/users.integration.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ afterAll(async () => {
2323
await db.destroy();
2424
});
2525

26+
beforeEach(async () => {
27+
await db.raw('BEGIN')
28+
});
29+
30+
afterEach(async () => {
31+
await db.raw('ROLLBACK')
32+
});
33+
2634
describe('GET /users', () => {
2735
it('should return a list of users', async () => {
2836
const response = await request(app).get('/users');

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"start": "ng serve --host 0.0.0.0 --port 4200",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
9-
"test": "ng test"
9+
"test": "ng test --no-watch"
1010
},
1111
"private": true,
1212
"dependencies": {

0 commit comments

Comments
 (0)