Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "node --watch src/index.js",
"start": "node src/index.js",
"seed": "node src/db/seed.js",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"test": "vitest run"
},
"dependencies": {
"@utpost/shared": "file:../shared",
Expand All @@ -21,6 +22,7 @@
"@types/express": "^4.17.0",
"@types/node": "^22.0.0",
"@types/pg": "^8.0.0",
"typescript": "^6.0.3"
"typescript": "^6.0.3",
"vitest": "^3.2.7"
}
}
18 changes: 17 additions & 1 deletion api/src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
// TODO: flytta ut det här nån gång. /marcus 2021-03-11
/* TODO: flytta ut det här nån gång. /marcus 2021-03-11
export const config = {
databaseUrl: 'postgres://utpost:utpost@localhost:5433/utpost',
jwtSecret: 'utpost-super-secret-2021',
port: 4000,
uploadDir: './uploads',
};

*/

//lösningen så testet blir grönt.
const jwtSecret = process.env.JWT_SECRET

if (!jwtSecret) {
throw new Error('JWT_SECRET måste vara satt')
}

export const config = {
databaseUrl: 'postgres://utpost:utpost@localhost:5433/utpost',
jwtSecret,
port: 4000,
uploadDir: './uploads',
}
26 changes: 26 additions & 0 deletions api/src/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Test som kollar att JWT_SECRET läses från miljön och att servern vägrar starta om den saknas.

import { describe, it, expect, vi } from 'vitest'

// config.js läser miljövariabler vid import, så vi laddar om modulen för varje test.
const loadConfig = async () => {
vi.resetModules()
return import('./config.js')
}
// TODO: Läs JWT_SECRET från process.env i config.js och kasta ett tydligt fel om den saknas.
// Ta bort den hårdkodade hemligheten och sätt JWT_SECRET vid lokal start och i CI.
describe('JWT-konfiguration', () => {
it('läser JWT_SECRET från miljön', async () => {
vi.stubEnv('JWT_SECRET', 'hemlighet-for-testet')

const { config } = await loadConfig()

expect(config.jwtSecret).toBe('hemlighet-for-testet')
})

it('vägrar starta om JWT_SECRET saknas', async () => {
vi.stubEnv('JWT_SECRET', '')

await expect(loadConfig()).rejects.toThrow(/JWT_SECRET/)
})
})
8 changes: 8 additions & 0 deletions api/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
environment: 'node',
unstubEnvs: true,
},
})
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "npm run lint --workspace=client",
"format:check": "npm run format:check --workspace=client",
"format": "npm run format --workspace=client",
"test": "npm test --workspace=client",
"test": "npm run test --workspace=client && npm run test --workspace=api",
"typecheck": "npm run typecheck --workspace=shared && npm run typecheck --workspace=api",
"build": "npm run build --workspace=web && npm run build --workspace=client"
},
Expand Down
Loading