Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding first e2e test #68

Merged
merged 4 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/server-CI.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Server CI

on: push

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: server

steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Install deps
run: yarn
MaxLeiter marked this conversation as resolved.
Show resolved Hide resolved
- name: Run tests
run: yarn test
3 changes: 2 additions & 1 deletion server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
__tests__/
5 changes: 5 additions & 0 deletions server/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
JWT_SECRET=secret-jwt
MEMORY_DB=true
REGISTRATION_PASSWORD=password
SECRET_KEY=secret
DRIFT_HOME=~/.drift-tests
MaxLeiter marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["<rootDir>/test/setup-tests.ts"],
moduleNameMapper: {
"@lib/(.*)": "<rootDir>/src/lib/$1",
"@routes/(.*)": "<rootDir>/src/routes/$1"
},
};
8 changes: 7 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"migrate:up": "ts-node migrate up",
"migrate:down": "ts-node migrate down",
"migrate": "ts-node migrate",
"lint": "prettier --config .prettierrc 'src/**/*.ts' 'index.ts' --write"
"lint": "prettier --config .prettierrc 'src/**/*.ts' 'index.ts' --write",
"test": "jest"
},
"author": "",
"license": "ISC",
Expand Down Expand Up @@ -41,12 +42,17 @@
"@types/cors": "^2.8.12",
"@types/express": "^4.0.39",
"@types/express-jwt": "^6.0.4",
"@types/jest": "^27.4.1",
"@types/jsonwebtoken": "^8.5.8",
"@types/marked": "^4.0.3",
"@types/node": "^17.0.21",
"@types/react-dom": "^17.0.14",
"@types/supertest": "^2.0.12",
"cross-env": "^7.0.3",
"jest": "^27.5.1",
"prettier": "^2.6.0",
"supertest": "^6.2.2",
"ts-jest": "^27.1.4",
"ts-node": "^10.6.0",
"tsc-alias": "^1.6.5",
"tsconfig-paths": "^3.14.1",
Expand Down
16 changes: 16 additions & 0 deletions server/src/__tests__/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as request from 'supertest'
import { app } from '../app'

describe('GET /health', () => {
it('should return 200 and a status up', (done) => {
request(app)
.get(`/health`)
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
if (err) return done(err)
expect(res.body).toMatchObject({ 'status': 'UP' })
done()
})
})
})
3 changes: 2 additions & 1 deletion server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as express from "express"
import * as bodyParser from "body-parser"
import * as errorhandler from "strong-error-handler"
import { posts, users, auth, files, admin } from "@routes/index"
import { posts, users, auth, files, admin, health } from "@routes/index"
import { errors } from "celebrate"
import secretKey from "@lib/middleware/secret-key"
import markdown from "@lib/render-markdown"
Expand All @@ -17,6 +17,7 @@ app.use("/posts", posts)
app.use("/users", users)
app.use("/files", files)
app.use("/admin", admin)
app.use("/health", health)

app.get("/welcome", secretKey, (req, res) => {
const introContent = process.env.WELCOME_CONTENT
Expand Down
2 changes: 1 addition & 1 deletion server/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const config = (): Config => {
}

const validNodeEnvs = (str: string | undefined) => {
const valid = ["development", "production"]
const valid = ["development", "production", "test"]
if (str && !valid.includes(str)) {
throw new Error(`Invalid NODE_ENV set: ${str}`)
} else if (!str) {
Expand Down
9 changes: 9 additions & 0 deletions server/src/routes/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from "express"

export const health = Router()

health.get("/", async (req, res) => {
return res.json({
status: "UP"
})
})
1 change: 1 addition & 0 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { posts } from "./posts"
export { users } from "./users"
export { files } from "./files"
export { admin } from "./admin"
export { health } from "./health"
4 changes: 4 additions & 0 deletions server/test/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as dotenv from 'dotenv';
import * as path from 'path';

dotenv.config({ path: path.resolve(process.cwd(), '.env.test') });
Loading