Skip to content

Commit 5bf7a40

Browse files
authored
Added more tests (#6)
* Fixed API and added more tests * Added more tests * Fixed workflow
1 parent 80a35dd commit 5bf7a40

File tree

33 files changed

+871
-202
lines changed

33 files changed

+871
-202
lines changed

.github/workflows/main.yml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ jobs:
1414
steps:
1515
- name: Checkout source code
1616
uses: actions/checkout@master
17+
- name: set inotify
18+
run: |
19+
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
1720
- name: docker hub login
1821
run: |
1922
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin $REGISTRY_DOMAIN
@@ -25,17 +28,17 @@ jobs:
2528
- name: Build api dev image
2629
run: |
2730
cd api
28-
docker build --no-cache . --target dev-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
31+
docker build . --target dev-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
2932
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
3033
- name: Build frontend dev image
3134
run: |
3235
cd frontend
33-
docker build --no-cache . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
36+
docker build . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
3437
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
3538
- name: Build backend dev image
3639
run: |
3740
cd backend
38-
docker build --no-cache . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
41+
docker build . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
3942
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
4043
- name: Build mysql dev image
4144
run: |
@@ -55,9 +58,21 @@ jobs:
5558
- name: Lint backend
5659
run: |
5760
docker exec backend npm run lint
61+
- name: Unit test backend
62+
run: |
63+
docker exec backend npm run test:unit
64+
- name: E2E test backend
65+
run: |
66+
docker exec backend npm run test:e2e -- --headless
5867
- name: Lint frontend
5968
run: |
6069
docker exec frontend npm run lint
70+
- name: Unit test frontend
71+
run: |
72+
docker exec frontend npm run test:unit
73+
- name: E2E test frontend
74+
run: |
75+
docker exec frontend npm run test:e2e -- --headless
6176
- name: Build nginx image
6277
if: github.ref == 'refs/heads/master'
6378
run: |
@@ -68,19 +83,19 @@ jobs:
6883
if: github.ref == 'refs/heads/master'
6984
run: |
7085
cd api
71-
docker build --no-cache . --target production-stage --build-arg NODE_ENV=production -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
86+
docker build . --target production-stage --build-arg NODE_ENV=production -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
7287
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
7388
- name: Build frontend latest image
7489
if: github.ref == 'refs/heads/master'
7590
run: |
7691
cd frontend
77-
docker build --no-cache . --target production-stage --build-arg NODE_ENV=production --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
92+
docker build . --target production-stage --build-arg NODE_ENV=production --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
7893
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
7994
- name: Build backend latest image
8095
if: github.ref == 'refs/heads/master'
8196
run: |
8297
cd backend
83-
docker build --no-cache . --target production-stage --build-arg NODE_ENV=production --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
98+
docker build . --target production-stage --build-arg NODE_ENV=production --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
8499
docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
85100
- name: Build mysql latest image
86101
if: github.ref == 'refs/heads/master'

.gitlab-ci.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ stages:
66
cache:
77
key: "$CI_PIPELINE_ID"
88
paths:
9+
- /root/.cache
910
- /root/.npm
1011
- node_modules
1112

@@ -20,7 +21,7 @@ build-nginx:
2021
<<: *before-script
2122
script:
2223
- cd nginx
23-
- docker build --no-cache . -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:nginx-dev
24+
- docker build . -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:nginx-dev
2425
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:nginx-dev
2526

2627
build-api:
@@ -29,7 +30,7 @@ build-api:
2930
<<: *before-script
3031
script:
3132
- cd api
32-
- docker build --no-cache . --target dev-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
33+
- docker build . --target dev-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
3334
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-dev
3435

3536
build-frontend:
@@ -41,7 +42,7 @@ build-frontend:
4142
BASE_URL: /frontend/
4243
script:
4344
- cd frontend
44-
- docker build --no-cache . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
45+
- docker build . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
4546
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-dev
4647

4748
build-backend:
@@ -53,7 +54,7 @@ build-backend:
5354
BASE_URL: /backend/
5455
script:
5556
- cd backend
56-
- docker build --no-cache . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
57+
- docker build . --target build-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
5758
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-dev
5859

5960
build-mysql:
@@ -81,16 +82,18 @@ testing-and-linting:
8182
- docker exec api npm run test
8283
- docker exec backend npm run lint
8384
- docker exec backend npm run test:unit
85+
- docker exec backend npm run test:e2e -- --headless
8486
- docker exec frontend npm run lint
8587
- docker exec frontend npm run test:unit
88+
- docker exec frontend npm run test:e2e -- --headless
8689

8790
production-api:
8891
stage: build production
8992
image: docker:stable
9093
<<: *before-script
9194
script:
9295
- cd api
93-
- docker build --no-cache . --target production-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
96+
- docker build . --target production-stage -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
9497
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:api-latest
9598

9699
production-backend:
@@ -102,7 +105,7 @@ production-backend:
102105
BASE_URL: /backend/
103106
script:
104107
- cd backend
105-
- docker build --no-cache . --target production-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
108+
- docker build . --target production-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/backend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
106109
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:backend-latest
107110

108111
production-frontend:
@@ -114,7 +117,7 @@ production-frontend:
114117
BASE_URL: /frontend/
115118
script:
116119
- cd frontend
117-
- docker build --no-cache . --target production-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
120+
- docker build . --target production-stage --build-arg NODE_ENV=$NODE_ENV --build-arg BASE_URL=/frontend/ -t $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
118121
- docker push $REGISTRY_DOMAIN/chrisleekr/nodejs-vuejs-mysql-boilerplate:frontend-latest
119122

120123
production-mysql:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ This is a boilerplate project. The project contains Node.js REST API and fronten
55
* API
66
* Node.js, Express, Webpack, Express Validator, JWT, Bunyan, Promise MySQL, Node Mailer, Jest, Supertest, Nodemon
77
* Frontend
8-
* Vue.js, Vuex, Vue Router, Vue Draggable, Vuelidate, BootstrapVue
8+
* Vue.js, Vuex, Vue Router, Vue Draggable, Vuelidate, BootstrapVue, Jest, Cypress
99
* Backend
10-
* Vue.js, Vuex, Vue Router, Vuelidate, BootstrapVue
10+
* Vue.js, Vuex, Vue Router, Vuelidate, BootstrapVue, Jest, Cypress
1111

1212
## Demo
1313

@@ -133,5 +133,5 @@ MySQL port is mapped to 3307.
133133

134134
## Todo
135135

136-
* [ ] Unit tests
137-
* [ ] E2E tests
136+
* [x] Unit tests
137+
* [x] E2E tests

api/app/controllers/__tests__/authController.test.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe('authController', () => {
1010
let request;
1111
let response;
1212

13+
const frontendURL = process.env.FRONTEND_URL;
14+
1315
beforeAll(async () => {
1416
request = supertest(app);
1517
});
@@ -366,9 +368,7 @@ describe('authController', () => {
366368
});
367369

368370
it('triggers expected header', () => {
369-
expect(response.header.location).toBe(
370-
'http://localhost/frontend/login?messageKey=registerConfirmValidationError'
371-
);
371+
expect(response.header.location).toBe(`${frontendURL}/login?messageKey=registerConfirmValidationError`);
372372
});
373373
});
374374

@@ -383,9 +383,7 @@ describe('authController', () => {
383383
});
384384

385385
it('triggers expected header', () => {
386-
expect(response.header.location).toBe(
387-
'http://localhost/frontend/login?messageKey=registerConfirmValidationError'
388-
);
386+
expect(response.header.location).toBe(`${frontendURL}/login?messageKey=registerConfirmValidationError`);
389387
});
390388
});
391389
});
@@ -409,7 +407,7 @@ describe('authController', () => {
409407
});
410408

411409
it('triggers expected header', () => {
412-
expect(response.header.location).toBe('http://localhost/frontend/login?messageKey=registerConfirmFailed');
410+
expect(response.header.location).toBe(`${frontendURL}/login?messageKey=registerConfirmFailed`);
413411
});
414412
});
415413

@@ -430,7 +428,7 @@ describe('authController', () => {
430428
});
431429

432430
it('triggers expected header', () => {
433-
expect(response.header.location).toBe('http://localhost/frontend/login?messageKey=registerConfirmSuccess');
431+
expect(response.header.location).toBe(`${frontendURL}/login?messageKey=registerConfirmSuccess`);
434432
});
435433
});
436434
});
@@ -625,7 +623,7 @@ describe('authController', () => {
625623

626624
it('triggers expected header', () => {
627625
expect(response.header.location).toBe(
628-
'http://localhost/frontend/password-reset-request?messageKey=passwordResetValidationError'
626+
`${frontendURL}/password-reset-request?messageKey=passwordResetValidationError`
629627
);
630628
});
631629
});
@@ -641,7 +639,7 @@ describe('authController', () => {
641639

642640
it('triggers expected header', () => {
643641
expect(response.header.location).toBe(
644-
'http://localhost/frontend/password-reset-request?messageKey=passwordResetValidationError'
642+
`${frontendURL}/password-reset-request?messageKey=passwordResetValidationError`
645643
);
646644
});
647645
});
@@ -658,7 +656,7 @@ describe('authController', () => {
658656

659657
it('triggers expected header', () => {
660658
expect(response.header.location).toBe(
661-
'http://localhost/frontend/password-reset-request?messageKey=passwordResetValidationError'
659+
`${frontendURL}/password-reset-request?messageKey=passwordResetValidationError`
662660
);
663661
});
664662
});
@@ -680,7 +678,7 @@ describe('authController', () => {
680678

681679
it('triggers expected header', () => {
682680
expect(response.header.location).toBe(
683-
`http://localhost/frontend/password-reset?messageKey=passwordResetTokenConfirmed&key=valid-key`
681+
`${frontendURL}/password-reset?messageKey=passwordResetTokenConfirmed&key=valid-key`
684682
);
685683
});
686684
});

api/app/server.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
require('dotenv').config();
2-
32
const express = require('express');
43
const cors = require('cors');
54
const helmet = require('helmet');
65
const compression = require('compression');
76
const bodyParser = require('body-parser');
8-
const audit = require('express-requests-logger');
7+
const bunyanMiddleware = require('bunyan-middleware');
98
const { logger } = require('./helpers/logger');
109

1110
const app = express();
1211

1312
const port = process.env.PORT || 3000;
14-
1513
app.set('trust proxy', true);
1614
app.use(helmet());
1715
app.use(cors());
1816
app.use(compression());
1917
app.use(
20-
audit({
18+
bunyanMiddleware({
19+
headerName: 'X-Request-Id',
20+
propertyName: 'reqId',
21+
logName: 'reqId',
22+
obscureHeaders: ['authorization'],
2123
logger,
22-
excludeURLs: [],
23-
request: {
24-
maskBody: ['password'],
25-
excludeHeaders: [],
26-
excludeBody: [],
27-
maskHeaders: ['authorization'],
28-
maxBodyLength: 0
29-
},
30-
response: {
31-
maskBody: ['authKey'],
32-
excludeHeaders: [],
33-
excludeBody: [],
34-
maskHeaders: [],
35-
maxBodyLength: 0
24+
additionalRequestFinishData: (_req, _res) => {
25+
return {};
3626
}
3727
})
3828
);

0 commit comments

Comments
 (0)