-
Hey folks, I am currently in the process of migration from jest to vitest in one of my projects. As far as I can tell most nodejs server applications are typically structured in a way, that they instantiate their database connection inside a plain file, export the connection and import it inside their controllers etc. Take for example this simplified db file: export const db = pgp({
host: process.env.POSTGRES_HOST ?? 'db',
port:
process.env.NODE_ENV === 'test'
? parseInt(process.env.POSTGRES_PORT ?? '5432')
: 5432,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
max: 30,
}) Now I would typically import the database inside my controllers like this: import { db } from 'db'
router.post('/', { /* some config */}, () => {
const result = await db.query('somequery');
}) This is all fine, but there is one major flaw I can't seem to overcome, when it comes to testing. export async function setupEnvironment(): Promise<IntegrationEnvironment> {
dbContainer = await new GenericContainer('postgres:14.2-alpine')
.withEnvironment({
POSTGRES_PASSWORD: POSTGRES_TEST_PASSWORD,
POSTGRES_USER: POSTGRES_TEST_USER,
POSTGRES_DB: POSTGRES_TEST_DB,
})
.withExposedPorts(POSTGRES_TESTING_HOST_PORT)
.start()
const dbPort = dbContainer.getMappedPort(POSTGRES_TESTING_HOST_PORT)
await runMigrations(dbPort)
process.env['VERSION'] = 'testing'
process.env['POSTGRES_HOST'] = dbContainer.getHost()
process.env['POSTGRES_PORT'] = `${dbPort}`
process.env['POSTGRES_DB'] = POSTGRES_TEST_DB
process.env['POSTGRES_USER'] = POSTGRES_TEST_USER
process.env['POSTGRES_PASSWORD'] = POSTGRES_TEST_PASSWORD
// Note the require here
const { server } = require('server/server')
appServer = server
// NOTE the require here
const { db, PostgresBgioStore } = require('server/db')
const servers = await server.run(0)
return {
dbContainer,
servers,
db,
PostgresBgioStore,
}
} As you can see there are two I was also thinking of finding a way to explicitely instantiated the database at a specific point in time and wire it with my server + routers. But I would then probably have to write a middleware doing so (I am using a koa server) and all the examples of typical koa/expressjs apps I've found typically don't seem to do this. Which is why I assume I am fundamentally misunderstanding something here. Any idea how I could fix this or what would be a good approach here? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think I've found a way: I declare the |
Beta Was this translation helpful? Give feedback.
I think I've found a way:
I declare the
db
withlet
and use a closure which is called at a specific time and only then binds the actual database objects to the exported variables. This seems to work fine, as far as I can tell.Still it would be nice to know, if there is some kind of best practices for things like this in the node world :)