testing best practices

Can anyone share how they're writing tests that involve drizzle? Looking for something similar to these docs for Prisma: https://www.prisma.io/docs/guides/testing/unit-testing
Prisma
Unit testing with Prisma
Learn how to setup and run unit tests with Prisma Client
5 Replies
DiamondDragon
DiamondDragon15mo ago
@johnnydt did you find a solution
johnnydt
johnnydtOP15mo ago
No
Angelelz
Angelelz15mo ago
If you want you write unit tests that involve Drizzle, you can mock like you would mock any thing else There's no point in testing library code in my opinion For database stuff, it's better to run integration tests in some docker or something like that Just my two cents
DiamondDragon
DiamondDragon15mo ago
ok i think i got a working solution. mine was a little more complicated since i am using pg_uuidv7 extension. But now this is running all my migration files to the docker container postgres server @johnnydt
import { PostgreSqlContainer } from '@testcontainers/postgresql'
import { sql } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import path from 'path'
import postgres from 'postgres'

export async function setupDockerTestDb() {
const POSTGRES_USER = 'test'
const POSTGRES_PASSWORD = 'test'
const POSTGRES_DB = 'test'

// Make sure to use Postgres 15 with pg_uuidv7 installed
// Ensure you have the pg_uuidv7 docker image locally
// You may need to modify pg_uuid's dockerfile to install the extension or build a new image from its base
// https://github.com/fboulnois/pg_uuidv7
const container = await new PostgreSqlContainer('pg_uuidv7')
.withEnvironment({
POSTGRES_USER: POSTGRES_USER,
POSTGRES_PASSWORD: POSTGRES_PASSWORD,
POSTGRES_DB: POSTGRES_DB,
})
.withExposedPorts(5432)
.start()

const connectionString = `postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${container.getHost()}:${container.getFirstMappedPort()}/${POSTGRES_DB}`
const client = postgres(connectionString)
const db = drizzle(client)

await db.execute(sql`CREATE EXTENSION IF NOT EXISTS pg_uuidv7`)
const migrationPath = path.join(process.cwd(), 'src/db/migrations')
await migrate(db, {
migrationsFolder: migrationPath,
})

const confirmDatabaseReady = await db.execute(sql`SELECT 1`)

return { container, db, confirmDatabaseReady, client }
}
import { PostgreSqlContainer } from '@testcontainers/postgresql'
import { sql } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import path from 'path'
import postgres from 'postgres'

export async function setupDockerTestDb() {
const POSTGRES_USER = 'test'
const POSTGRES_PASSWORD = 'test'
const POSTGRES_DB = 'test'

// Make sure to use Postgres 15 with pg_uuidv7 installed
// Ensure you have the pg_uuidv7 docker image locally
// You may need to modify pg_uuid's dockerfile to install the extension or build a new image from its base
// https://github.com/fboulnois/pg_uuidv7
const container = await new PostgreSqlContainer('pg_uuidv7')
.withEnvironment({
POSTGRES_USER: POSTGRES_USER,
POSTGRES_PASSWORD: POSTGRES_PASSWORD,
POSTGRES_DB: POSTGRES_DB,
})
.withExposedPorts(5432)
.start()

const connectionString = `postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${container.getHost()}:${container.getFirstMappedPort()}/${POSTGRES_DB}`
const client = postgres(connectionString)
const db = drizzle(client)

await db.execute(sql`CREATE EXTENSION IF NOT EXISTS pg_uuidv7`)
const migrationPath = path.join(process.cwd(), 'src/db/migrations')
await migrate(db, {
migrationsFolder: migrationPath,
})

const confirmDatabaseReady = await db.execute(sql`SELECT 1`)

return { container, db, confirmDatabaseReady, client }
}
Want results from more Discord servers?
Add your server