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
D
DiamondDragon229d ago
@johnnydt did you find a solution
J
johnnydt229d ago
No
A
Angelelz229d 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
D
DiamondDragon228d 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
More Posts
supabase workflowHey I'm trying to set up a sveltekit app with supabase + drizzle and currently what I got going is aError: self signed certificate in certificate chainHello, I am a new user of drizzle and trying to run the command for the first time: ```bash drizzle-insert with InferModel not workingHey, playing with Drizzle, like it a lot but I am having an issue where I inferred the model of a scmysql-core fails on TS build on 0.27.1Getting this error when I try to build my ts node appUse a different schema on postgresHello, I am currently trying to define a table with drizzle like this: ```typescript const rollupAcSpecifying foreign key names in SchemaAfter introspecting from a database, and generating new schema with changes, the existing foreign keInternal server error: Error connecting to database: fetch failedI'm trying to send the following query to my db but it eventually times out, throwing an error: ```ERR_PACKAGE_PATH_NOT_EXPORTED on 0.27.1I'm getting the following message using drizzle-orm `0.27.1`. I've tried using various different nodError: Error connecting to database: fetch is not definedTrying to use neon-http with SST ``` import { neonConfig, neon, Pool } from "@neondatabase/serverlegood way to get the "count" for paginated queriesI'm doing the following to get the total count and the values in a paginated query: ``` const fileCoSqlite3 support for DenoApologies if I'm being dense, but is there support for deno with a local sqlite3 db? I can't seem topostges, auto-generated uuid as primary keyI'm looking to change all my tables from having a serial int primary key as ID what is the best way Relational query `extras` are not properly adding sqlWhen I try to use the drizzle orm relational query like it is show in the docs I run into an error. Schema not ingested with drizzle-orm/node-postgresI have a db connection for production using Vercel Postgres. It works great. I'm trying to set up a Extract interface for table from schemaCan I extract an interface for a table from my schema without using it from the client? Something liIncluding more than 1 relation in query throws errorIf i try to include more than 1 relation i get an error 👇 ``` { "errorMessage": "could not ideNew neon http driver not working with pooled connectionsis the new `{ drizzle } from "drizzle-orm/neon-http"` not compatible with pooled connections? GettinPostgres functionsHey, I'm migrating a ton of postgres raw sql into using drizzle ORM. Is there any sort of setup for