Skip to content

Commit

Permalink
feat: implemented script to test both rest api, express and fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jul 6, 2022
1 parent e1f8b32 commit 9d651d8
Show file tree
Hide file tree
Showing 22 changed files with 383 additions and 1,262 deletions.
31 changes: 31 additions & 0 deletions example/express-graphql/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests - example/express-graphql

on: [push]

jobs:
test:
name: Testing Simba.js API
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x

- name: Install Node.js dependencies
run: npm ci

- name: Revert changes into the package-lock.json file
run: git checkout -- package-lock.json

- name: Run test
run: npm run test:ci
env:
MONGO_URI: ${{ secrets.MONGO_URI }}
NODE_ENV: ci
8 changes: 4 additions & 4 deletions example/express-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"dependencies": {
"@graphql-tools/schema": "^8.5.0",
"@sinclair/typebox": "^0.24.9",
"@sinclair/typebox": "^0.24.10",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"apollo-server-core": "^3.9.0",
Expand All @@ -27,7 +27,7 @@
"express-pino-logger": "^7.0.0",
"graphql": "^16.5.0",
"http-errors": "^2.0.0",
"mongoose": "^6.4.2",
"mongoose": "^6.4.3",
"pino-pretty": "^8.1.0",
"swagger-ui-express": "^4.4.0"
},
Expand All @@ -38,7 +38,7 @@
"@types/express-pino-logger": "^4.0.3",
"@types/http-errors": "^1.8.2",
"@types/jest": "^28.1.4",
"@types/node": "^18.0.1",
"@types/node": "^18.0.3",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
Expand All @@ -55,7 +55,7 @@
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.1.2",
"jest-unit": "^0.0.1",
"nodemon": "^2.0.18",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-jest": "^28.0.5",
Expand Down
55 changes: 55 additions & 0 deletions example/express-graphql/src/database/mongo/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { connect, connection } from 'mongoose'
import { HttpLogger } from 'express-pino-logger'

const ENVIRONMENTS_WITHOUT_RECONNECTION = ['ci', 'local']
const dbConnection = async (
logger: HttpLogger['logger']
): Promise<{
connect: () => Promise<typeof import('mongoose')>
disconnect: () => Promise<void>
}> => {
const connectionConfig = {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
}

connection.on('connected', () => {
logger.info('Mongo connection established.')
})
connection.on('reconnected', () => {
logger.info('Mongo connection reestablished')
})
connection.on('disconnected', () => {
if (
!ENVIRONMENTS_WITHOUT_RECONNECTION.includes(
process.env.NODE_ENV as string
)
) {
logger.info(
'Mongo connection disconnected. Trying to reconnected to Mongo...'
)
setTimeout(() => {
connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
}
})
connection.on('close', () => {
logger.info('Mongo connection closed')
})
connection.on('error', (e: Error) => {
logger.info('Mongo connection error:')
logger.error(e)
})

return {
connect: () => connect(process.env.MONGO_URI as string, connectionConfig),
disconnect: () => connection.close()
}
}

export { dbConnection }
1 change: 1 addition & 0 deletions example/express-graphql/src/database/mongo/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './models'
export * from './queries'
export * from './connection'
Loading

0 comments on commit 9d651d8

Please sign in to comment.