Skip to content

Commit

Permalink
test: init backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-eren committed Sep 4, 2024
1 parent 09c8f99 commit fafe8d7
Show file tree
Hide file tree
Showing 8 changed files with 1,791 additions and 18 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [push, pull_request]
permissions: read-all

jobs:
lint:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -27,3 +27,7 @@ jobs:
- name: Lint
run: yarn lint
working-directory: backend

- name: Test
run: yarn test
working-directory: backend
5 changes: 5 additions & 0 deletions backend/.eslintrc.js → backend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ module.exports = {
'@typescript-eslint/no-restricted-imports': ['error'],
},
},
{
files: ['test/**'],
plugins: ['vitest'],
extends: ['plugin:vitest/legacy-recommended'],
},
{
files: ['*'],
plugins: ['prettier'],
Expand Down
17 changes: 14 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "backend",
"type": "module",
"scripts": {
"prepare": "yarn drizzle:generate",
"start": "tsx src/index.ts",
"typecheck": "tsc --noEmit",
"drizzle:generate": "drizzle-kit generate",
"prepare": "yarn drizzle:generate",
"test": "vitest",
"coverage": "vitest run --coverage",
"typecheck": "tsc --noEmit",
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint . --max-warnings=0",
Expand All @@ -18,9 +21,11 @@
"postgres": "^3.4.4"
},
"devDependencies": {
"@testcontainers/postgresql": "^10.13.0",
"@types/node": "^22.5.3",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^8.4.0",
"@vitest/coverage-istanbul": "^2.0.5",
"drizzle-kit": "^0.24.2",
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -29,8 +34,14 @@
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-unused-imports": "^3.2.0",
"eslint-plugin-vitest": "^0.5.4",
"prettier": "^3.2.5",
"tsx": "^4.19.0",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.0.5"
},
"resolutions": {
"strip-ansi": "6.0.1"
}
}
2 changes: 1 addition & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type AppConfiguration = {
}
}

async function buildApp(config: AppConfiguration): Promise<FastifyInstance> {
export async function buildApp(config: AppConfiguration): Promise<FastifyInstance> {
const app = Fastify({ logger: true })

dotenv.config()
Expand Down
27 changes: 27 additions & 0 deletions backend/test/health.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { type StartedPostgreSqlContainer } from '@testcontainers/postgresql'
import { type FastifyInstance } from 'fastify'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'

import { startTestAppInstance, stopTestAppInstance } from './utils/fixtures'

describe('GET /health route', () => {
let container: StartedPostgreSqlContainer
let app: FastifyInstance

beforeAll(async () => {
;({ container, app } = await startTestAppInstance())
})

afterAll(async () => {
await stopTestAppInstance(container, app)
})

test('should return success', async () => {
const response = await app.inject({
method: 'GET',
url: '/health',
})

expect(response.statusCode).toBe(200)
})
})
26 changes: 26 additions & 0 deletions backend/test/utils/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PostgreSqlContainer, type StartedPostgreSqlContainer } from '@testcontainers/postgresql'
import type { FastifyInstance } from 'fastify'

import { buildApp } from '@/app'

export async function startTestAppInstance() {
const container = await new PostgreSqlContainer().start()

const app = await buildApp({
database: {
connectionString: container.getConnectionUri(),
},
app: {
port: 8080,
},
})

await app.ready()

return { container, app }
}

export async function stopTestAppInstance(container: StartedPostgreSqlContainer, app: FastifyInstance) {
await app?.close()
await container?.stop()
}
15 changes: 15 additions & 0 deletions backend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable import/no-unused-modules */

import tsconfigPaths from 'vite-tsconfig-paths'
import { defineConfig } from 'vitest/config'

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
hookTimeout: 50_000,
coverage: {
provider: 'istanbul',
reporter: ['text', 'json-summary', 'json'],
},
},
})
Loading

0 comments on commit fafe8d7

Please sign in to comment.