Skip to content

Commit

Permalink
✅ add tests, 2/2 passing
Browse files Browse the repository at this point in the history
  • Loading branch information
thinktapper committed Nov 20, 2022
1 parent 1a53b3d commit 8318b7d
Show file tree
Hide file tree
Showing 11 changed files with 7,078 additions and 1,448 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
8,431 changes: 6,993 additions & 1,438 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "Custom server + API for Dinder",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.ts"
"test": "jest",
"dev": "nodemon src/index.ts",
"build": "tsc -p tsconfig.json",
"start": "node dist/index.js"
},
"repository": {
"type": "git",
Expand All @@ -20,12 +22,18 @@
"dependencies": {
"@prisma/client": "^4.6.1",
"@trpc/server": "^10.0.0-rc.8",
"@types/jest": "^29.2.3",
"@types/supertest": "^2.0.12",
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-validator": "^6.14.2",
"jest": "^29.3.1",
"jsonwebtoken": "^8.5.1",
"lodash.merge": "^4.6.2",
"morgan": "^1.10.0",
"supertest": "^6.3.1",
"ts-jest": "^29.0.3",
"zod": "^3.19.1"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/__test__/routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import app from '../server'
import supertest from 'supertest'

describe('GET /', () => {
it('should send back some data', async () => {
const res = await supertest(app).get('/')

expect(res.body.message).toBe('Hello World!')
})
})
31 changes: 31 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import merge from 'lodash.merge'

// make sure default NODE_ENV is set
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

// set the stage
const stage = process.env.STAGE || 'local'
let envConfig

// dynamically require each config depending on the stage we're in
if (stage === 'production') {
envConfig = require('./prod').default
} else if (stage === 'staging') {
envConfig = require('./staging').default
} else {
envConfig = require('./local').default
}

// merge the config with the default config
const defaultConfig = {
stage,
env: process.env.NODE_ENV,
port: 3000,
secrets: {
jwt: process.env.JWT_SECRET,
dbUrl: process.env.DATABASE_URL,
},
logging: false,
}

export default merge(defaultConfig, envConfig)
3 changes: 3 additions & 0 deletions src/config/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
port: 3000,
}
3 changes: 3 additions & 0 deletions src/config/prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
port: process.env.PORT,
}
Empty file added src/config/staging.ts
Empty file.
14 changes: 14 additions & 0 deletions src/handlers/__test__/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as user from '../user'

describe('user handler', () => {
it('should create a new user', async () => {
const req = { body: { username: 'hello', password: 'hi' } }
const res = {
json({ token }) {
expect(token).toBeTruthy()
},
}

await user.signup(req, res, () => {})
})
})
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as dotenv from 'dotenv'
dotenv.config()

import config from './config'
import app from './server'

const port = process.env.PORT || 3001

app.listen(port, () => {
console.log('Server is running on port', port)
app.listen(config.port, () => {
console.log('Server is running on port', config.port)
})
9 changes: 6 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
// "strict": true,
"rootDir": "./src",
"strict": false,
"lib": ["esnext"],
"esModuleInterop": true
}
"esModuleInterop": true,
"declaration": true
},
"include": ["src/**/*.ts"]
}

0 comments on commit 8318b7d

Please sign in to comment.