Skip to content

Commit

Permalink
feat: add e2e testing (#6)
Browse files Browse the repository at this point in the history
* feat: add e2e testing when pushing

* feat: add health check

* fix: test command

* fix: update attempts for health check

* feat: show log when failure

* fix: test

* feat: update version

* docs(readme): update usage
  • Loading branch information
Notekunn authored Nov 14, 2022
1 parent f70c286 commit ebbefe2
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 61 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SERVICE_HOST=0.0.0.0
SERVICE_PORT=3333

DB_PORT=localhost
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=admin
DB_PASSWORD=admin
Expand All @@ -13,3 +13,8 @@ DB_AUTO_SYNC=false

JWT_SECRET_KEY=top_secret
JWT_EXPIRATION_TIME=7d

# For testing
API_URL=http://localhost:3333
USER_EMAIL=user@test.com
USER_PASSWORD=Abcd12345-
63 changes: 54 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,66 @@ on:
- '!main'

jobs:
lint:
name: Run linter
test:
name: Run test
runs-on: ubuntu-latest
timeout-minutes: 20
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2.3.4

- uses: actions/setup-node@v2.1.5
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install package
run: yarn

- name: Check lint
run: yarn lint

- name: Run API in background
run: |
yarn build
node dist/main > .log &
echo $! > .pid
env:
SERVICE_HOST: 0.0.0.0
SERVICE_PORT: 3000
DB_HOST: localhost
DB_PORT: ${{ job.services.postgres.ports['5432'] }}
DB_USERNAME: postgres
DB_PASSWORD: postgres
DB_NAME: postgres
DB_AUTO_RUN_MIGRATIONS: 'false'
DB_AUTO_SYNC: 'true'
JWT_SECRET_KEY: top_secret
JWT_EXPIRATION_TIME: 7d
- name: Healthcheck API
uses: jtalk/url-health-check-action@v3
with:
url: http://localhost:3000/v1/healthz
max-attempts: 10
retry-delay: 5s
retry-all: false
- name: Run test
run: yarn test
run: yarn test:e2e
env:
API_URL: http://localhost:3000
USER_EMAIL: user@test.com
USER_PASSWORD: Abcd12345-
- name: Show Log
if: failure()
run: cat .log
- name: Kill API
run: kill $(cat .pid)
if: always()
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<a href="https://cloud.google.com/kubernetes-engine" target="blank"><img src="https://raw.githubusercontent.com/kubernetes/kubernetes/master/logo/logo.png" height="80" alt="Kubernetes Logo" /></a>
<a href="https://commitlint.js.org/" target="blank"><img src="https://raw.githubusercontent.com/conventional-changelog/commitlint/master/docs/assets/icon.svg" height="80" alt="CommitLint Logo" /></a>
<a href="https://semantic-release.gitbook.io/semantic-release/" target="blank"><img src="https://raw.githubusercontent.com/semantic-release/semantic-release/master/media/semantic-release-logo.svg" height="80" alt="Semantic Release Logo" /></a>
<a href="https://github.com/nestjs/swagger" target="blank"><img src="https://raw.githubusercontent.com/swagger-api/swagger-ui/master/dist/favicon-32x32.png" height="80" alt="CommitLint Logo" /></a>
<a href="https://github.com/nestjs/swagger" target="blank"><img src="https://raw.githubusercontent.com/swagger-api/swagger-ui/master/dist/favicon-32x32.png" height="80" alt="CommitLint Logo" /></a>
<a href="https://www.fastify.io/" target="blank"><img src="https://github.com/fastify/graphics/raw/HEAD/fastify-landscape-outlined.svg" height="80" alt="Fastify Logo" /></a>
</p>

Expand All @@ -29,33 +29,34 @@
## Installation

```bash
$ npm install
$ yarn install
```

## Running the app

```bash
# development
$ npm run start
$ yarn start

# watch mode
$ npm run start:dev
$ yarn start:dev

# production mode
$ npm run start:prod
$ yarn start:prod
```

## Test

```bash
# unit tests
$ npm run test
$ yarn test

# e2e tests
$ npm run test:e2e
$ yarn start
$ yarn test:e2e

# test coverage
$ npm run test:cov
$ yarn test:cov
```

## TypeORM
Expand Down
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [x] Add github CI-CD for lint
- [x] Setup semantic release
- [x] Update readme
- [ ] Implement base
- [ ] Implement TypeORM
- [x] Implement base
- [x] Implement TypeORM
- [x] Implement e2e testing
- [ ] Add hygen
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:ci": "nest start",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js -d ./src/ormconfig.ts",
"migration:generate": "f() { yarn build && yarn typeorm migration:generate -p \"./src/databases/migrations/$@\"; }; f",
"migration:run": "yarn build && yarn typeorm migration:run",
Expand All @@ -27,7 +28,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.config.js --forceExit"
},
"dependencies": {
"@fastify/helmet": "^10.0.2",
Expand All @@ -45,6 +46,7 @@
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"env-cmd": "^10.1.0",
"morgan": "^1.10.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/modules/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class AuthController {
type: LoginResponseDto,
status: 200,
})
@HttpCode(200)
@Post('login')
async login(@Request() req) {
const user = req.user
Expand Down
19 changes: 3 additions & 16 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import * as request from 'supertest'

import { AppModule } from './../src/app.module'

describe('AppController (e2e)', () => {
let app: INestApplication

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile()

app = moduleFixture.createNestApplication()
await app.init()
})
const apiURL = process.env.API_URL

it('/healthz (GET)', () => {
return request(app.getHttpServer()).get('/healthz').expect(200)
it('/v1/healthz (GET)', () => {
return request(apiURL).get('/v1/healthz').expect(200)
})
})
25 changes: 25 additions & 0 deletions test/auth/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as request from 'supertest'

describe('Auth (e2e)', () => {
const { API_URL, USER_EMAIL, USER_PASSWORD } = process.env

it('register', () => {
return request(API_URL)
.post('/v1/auth/register')
.send({
email: USER_EMAIL,
password: USER_PASSWORD,
})
.expect(201)
})

it('login', () => {
return request(API_URL)
.post('/v1/auth/login')
.send({
email: USER_EMAIL,
password: USER_PASSWORD,
})
.expect(200)
})
})
14 changes: 14 additions & 0 deletions test/jest-e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { pathsToModuleNameMapper } = require('ts-jest')
const { compilerOptions } = require('../tsconfig.json')

module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testEnvironment: 'node',
testRegex: '.e2e-spec.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/..' }),
}
9 changes: 0 additions & 9 deletions test/jest-e2e.json

This file was deleted.

29 changes: 15 additions & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"paths": {
"@common/*": ["src/common/*"],
"@constants/*": ["src/constants/*"],
"@decorators/*": ["src/decorators/*"],
"@exceptions/*": ["src/exceptions/*"],
"@filters/*": ["src/filters/*"],
"@guards/*": ["src/guards/*"],
"@i18n/*": ["src/i18n/*"],
"@interceptors/*": ["src/interceptors/*"],
"@interfaces/*": ["src/interfaces/*"],
"@middlewares/*": ["src/middlewares/*"],
"@modules/*": ["src/modules/*"],
"@providers/*": ["src/providers/*"],
"@shared/*": ["src/shared/*"],
"@views/*": ["src/views/*"]
"@root/*": ["./src/*"],
"@common/*": ["./src/common/*"],
"@constants/*": ["./src/constants/*"],
"@decorators/*": ["./src/decorators/*"],
"@exceptions/*": ["./src/exceptions/*"],
"@filters/*": ["./src/filters/*"],
"@guards/*": ["./src/guards/*"],
"@i18n/*": ["./src/i18n/*"],
"@interceptors/*": ["./src/interceptors/*"],
"@interfaces/*": ["./src/interfaces/*"],
"@middlewares/*": ["./src/middlewares/*"],
"@modules/*": ["./src/modules/*"],
"@providers/*": ["./src/providers/*"],
"@shared/*": ["./src/shared/*"],
"@views/*": ["./src/views/*"]
}
}
}
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,7 @@ combined-stream@^1.0.8:
dependencies:
delayed-stream "~1.0.0"

commander@4.1.1:
commander@4.1.1, commander@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==
Expand Down Expand Up @@ -3385,6 +3385,14 @@ env-ci@^5.0.0:
fromentries "^1.3.2"
java-properties "^1.0.0"

env-cmd@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/env-cmd/-/env-cmd-10.1.0.tgz#c7f5d3b550c9519f137fdac4dd8fb6866a8c8c4b"
integrity sha512-mMdWTT9XKN7yNth/6N6g2GuKuJTsKMDHlQFUDacb/heQRRWOTIZ42t1rMHnQu4jYxU1ajdTeJM+9eEETlqToMA==
dependencies:
commander "^4.0.0"
cross-spawn "^7.0.0"

env-paths@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
Expand Down

0 comments on commit ebbefe2

Please sign in to comment.