Skip to content

Commit

Permalink
feat: implemented tests for fastify-rest example
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jul 5, 2022
1 parent 5f69db4 commit 7a75b0b
Show file tree
Hide file tree
Showing 15 changed files with 2,453 additions and 204 deletions.
42 changes: 37 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
{
"env": {
"node": true
"node": true,
"jest/globals": true
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"import",
"prettier"
"prettier",
"jest"
],
"extends": [
"standard",
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-interface": "off",
"arrow-parens": [
"error",
"as-needed"
],
"import/extensions": [
2,
{
"ts": "never",
"js": "always",
"json": "always"
}
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true
"devDependencies": [
"**/*.test.ts",
"webpack.config.js",
"jest.config.ts"
],
"optionalDependencies": [
"**/*.test.ts"
],
"peerDependencies": [
"**/*.test.ts"
]
}
],
"max-len": [
Expand All @@ -40,7 +66,10 @@
"error",
"always"
],
"object-shorthand": ["error", "always"],
"object-shorthand": [
"error",
"always"
],
"prefer-const": "error",
"prettier/prettier": [
"error",
Expand All @@ -59,6 +88,9 @@
"error",
"as-needed"
],
"spaced-comment": ["error", "always"]
"spaced-comment": [
"error",
"always"
]
}
}
51 changes: 40 additions & 11 deletions example/fastify/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"env": {
"node": true
"node": true,
"jest/globals": true
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "import", "prettier"],
"plugins": [
"@typescript-eslint",
"import",
"prettier",
"jest"
],
"extends": [
"standard",
"eslint:recommended",
Expand All @@ -15,7 +21,10 @@
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-interface": "off",
"arrow-parens": ["error", "as-needed"],
"arrow-parens": [
"error",
"as-needed"
],
"import/extensions": [
2,
{
Expand All @@ -27,9 +36,17 @@
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": ["**/*.test.js", "webpack.config.js"],
"optionalDependencies": ["**/*.test.js"],
"peerDependencies": ["**/*.test.js"]
"devDependencies": [
"**/*.test.ts",
"webpack.config.js",
"jest.config.ts"
],
"optionalDependencies": [
"**/*.test.ts"
],
"peerDependencies": [
"**/*.test.ts"
]
}
],
"max-len": [
Expand All @@ -45,8 +62,14 @@
}
],
"newline-before-return": "error",
"object-curly-spacing": ["error", "always"],
"object-shorthand": ["error", "always"],
"object-curly-spacing": [
"error",
"always"
],
"object-shorthand": [
"error",
"always"
],
"prefer-const": "error",
"prettier/prettier": [
"error",
Expand All @@ -61,7 +84,13 @@
"trailingComma": "none"
}
],
"radix": ["error", "as-needed"],
"spaced-comment": ["error", "always"]
"radix": [
"error",
"as-needed"
],
"spaced-comment": [
"error",
"always"
]
}
}
}
1 change: 1 addition & 0 deletions example/fastify/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ typings/
# End of https://www.toptal.com/developers/gitignore/api/node,yarn

dist
test/setEnvVars.ts
18 changes: 18 additions & 0 deletions example/fastify/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Config } from '@jest/types'

const config: Config.InitialOptions = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 1 * 60 * 1000,
globalSetup: './test/jestGlobalSetup.ts',
modulePaths: ['<rootDir>/src', '<rootDir>/node_modules'],
roots: ['.'],
moduleFileExtensions: ['js', 'json', 'ts'],
testRegex: '.test.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest'
}
}

export default config
28 changes: 19 additions & 9 deletions example/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,48 @@
"lint": "eslint src/* --ext .ts --fix",
"service": "nodemon",
"start": "node dist/index.js",
"release": "standard-version"
"release": "standard-version",
"test:ci": "jest --ci -i",
"test:local": "NODE_ENV=local jest --ci -i"
},
"author": "AnthonyLzq <sluzquinosa@uni.pe>",
"license": "MIT",
"dependencies": {
"@fastify/cors": "^7",
"@fastify/swagger": "^6",
"@sinclair/typebox": "^0.23.5",
"@sinclair/typebox": "^0.24.9",
"ajv": "^6",
"fastify": "^3",
"http-errors": "^2.0.0",
"mongoose": "^6.4.0",
"mongoose": "^6.4.2",
"pino-pretty": "^8.1.0"
},
"devDependencies": {
"@jest/types": "^28.1.1",
"@types/http-errors": "^1.8.2",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"@types/jest": "^28.1.4",
"@types/node": "^18.0.1",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"axios": "^0.27.2",
"dotenv": "^16.0.1",
"eslint": "^8.18.0",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-n": "^15.2.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.1.2",
"jest-unit": "^0.0.1",
"nodemon": "^2.0.18",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-jest": "^28.0.5",
"ts-loader": "^9.3.1",
"ts-node": "^10.8.1",
"ts-node": "^10.8.2",
"tsconfig-paths": "^4.0.0",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"typescript": "^4.7.4",
Expand Down
32 changes: 20 additions & 12 deletions example/fastify/src/network/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class Server {

constructor() {
this.#app = fastify({
logger: { prettyPrint: process.env.NODE_ENV !== 'production' }
logger: {
prettyPrint: !['production', 'ci'].includes(
process.env.NODE_ENV as string
)
}
})
this.#config()
}
Expand Down Expand Up @@ -47,15 +51,18 @@ class Server {
this.#app.log.info('Mongo connection reestablished')
})
this.#connection.on('disconnected', () => {
this.#app.log.info('Mongo connection disconnected')
this.#app.log.info('Trying to reconnected to Mongo...')
setTimeout(() => {
mongoose.connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
if (!['ci', 'local'].includes(process.env.NODE_ENV as string)) {
this.#app.log.info(
'Mongo connection disconnected. Trying to reconnected to Mongo...'
)
setTimeout(() => {
mongoose.connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
}
})
this.#connection.on('close', () => {
this.#app.log.info('Mongo connection closed')
Expand All @@ -69,17 +76,18 @@ class Server {

public async start(): Promise<void> {
try {
await this.#mongo()
await this.#app.listen(PORT)
this.#mongo()
} catch (e) {
console.error(e)
}
}

public async stop(): Promise<void> {
try {
if (this.#connection) await this.#connection.close()

await this.#app.close()
process.exit(0)
} catch (e) {
console.error(e)
}
Expand Down
Loading

0 comments on commit 7a75b0b

Please sign in to comment.