Skip to content

Commit

Permalink
Add unit tests setup, fix integration test concurrently issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksTeresh committed May 14, 2024
1 parent 7af8336 commit 8f13f33
Show file tree
Hide file tree
Showing 7 changed files with 1,188 additions and 2,681 deletions.
3,799 changes: 1,128 additions & 2,671 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
"start:dev": "NODE_ENV=development concurrently \"nodemon --exec node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm src/server/index.ts\" \"vite\"",
"start:dev:front": "vite",
"start:dev:server": "NODE_ENV=development ts-node-dev src/server/index.ts",
"test:integration": "concurrently \"docker run -e PGDATA=/test-data -e POSTGRES_PASSWORD=postgres -p 5433:5432 postgres:15.6\" \"NODE_ENV=test PORT=8001 DATABASE_URL=postgres://postgres:postgres@localhost:5433/postgres node --experimental-vm-modules node_modules/jest/bin/jest.js --setupFilesAfterEnv ./setupIntegrationTests.ts\" --hide 0 --prefix none",
"test:integration:watch": "concurrently \"docker run -e PGDATA=/test-data -e POSTGRES_PASSWORD=postgres -p 5433:5432 postgres:15.6\" \"NODE_ENV=test PORT=8001 DATABASE_URL=postgres://postgres:postgres@localhost:5433/postgres node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --setupFilesAfterEnv ./setupIntegrationTests.ts\" --hide 0 --prefix none",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testMatch '**/*.test.*' --setupFilesAfterEnv ./setupTests.ts",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testMatch '**/*.test.*' --setupFilesAfterEnv ./setupTests.ts --watchAll",
"test:integration": "concurrently \"docker run -e PGDATA=/test-data -e POSTGRES_PASSWORD=postgres -p 5433:5432 postgres:15.6\" \"NODE_ENV=test PORT=8001 DATABASE_URL=postgres://postgres:postgres@localhost:5433/postgres node --experimental-vm-modules node_modules/jest/bin/jest.js --setupFilesAfterEnv ./setupIntegrationTests.ts --forceExit\" --kill-others --success command-1 --hide 0 --prefix none",
"test:integration:watch": "concurrently \"docker run -e PGDATA=/test-data -e POSTGRES_PASSWORD=postgres -p 5433:5432 postgres:15.6\" \"NODE_ENV=test PORT=8001 DATABASE_URL=postgres://postgres:postgres@localhost:5433/postgres node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll --setupFilesAfterEnv ./setupIntegrationTests.ts\" --hide 0 --prefix none",
"lint": "eslint 'src/**/*.{ts,tsx}'",
"format": "prettier --write '*.{ts,tsx,json,css,md}'",
"build": "DISABLE_ESLINT_PLUGIN=true vite build",
"prepare": "husky install"
},
"jest": {
"testMatch": [
"**/*.integration-test.ts"
"**/*.integration-test.*"
],
"extensionsToTreatAsEsm": [
".ts"
Expand Down Expand Up @@ -68,6 +70,7 @@
"@babel/preset-react": "^7.24.1",
"@babel/preset-typescript": "^7.24.1",
"@babel/register": "^7.23.7",
"@jest/globals": "^29.7.0",
"@types/connect-redis": "^0.0.23",
"@types/cors": "^2.8.17",
"@types/eslint": "^8.56.9",
Expand Down Expand Up @@ -151,9 +154,10 @@
"error",
{
"devDependencies": [
"**/*.integration-test.ts",
"**/*.test.ts",
"**/*.test.tsx"
"**/*.integration-test.*",
"**/*.test.*",
"./setupTests.ts",
"./setupIntegrationTests.ts"
]
}
]
Expand Down
8 changes: 6 additions & 2 deletions setupIntegrationTests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { jest } from '@jest/globals'
import * as db from './src/server/db/connection'

global.jest = jest

global.beforeEach(async () => {
await db.runMigrations()
})
// retry to connect to the DB in case the test runner starts before the DB is ready
await db.connectToDatabase(2)
}, 10000)

global.afterEach(async () => {
await db.resetDatabase()
Expand Down
3 changes: 3 additions & 0 deletions setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { jest } from '@jest/globals'

global.jest = jest
6 changes: 5 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') {

app.listen(PORT, async () => {
await connectToDatabase()
await seed()

// only seed when in dev environment
if (process.env.NODE_ENV === 'development') {
await seed()
}

await setupAuthentication()

Expand Down
34 changes: 34 additions & 0 deletions src/server/validators/thesis.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { validateThesisData } from './thesis'

describe('validateThesisData', () => {
let req
let res
let next

beforeEach(() => {
req = {
body: {},
}
res = {}
next = jest.fn()
})

it('should call next if all required fields are present', () => {
req.body = {
topic: 'Test thesis',
supervisions: [{ percentage: 100 }],
authors: [{}],
researchPlan: {},
waysOfWorking: {},
startDate: '2021-01-01',
endDate: '2021-12-31',
}
req.files = {
researchPlan: [{}],
waysOfWorking: [{}],
}

expect(validateThesisData(req, res, next)).toBeUndefined();
expect(next).toHaveBeenCalledTimes(1)
})
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"include": [
"src/**/*",
"types/@unfuck-utf8-headers-middleware.d..ts",
"setupIntegrationTests.ts"
"setupIntegrationTests.ts",
"setupTests.ts"
],
"references": [{ "path": "./tsconfig.node.json" }],
"ts-node": {
Expand Down

0 comments on commit 8f13f33

Please sign in to comment.