ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Teamโ€ข3y agoโ€ข
11 replies
baronnoraz

Unit Testing with Transactions

Typically in our Unit Tests, we have something like...
describe('test suite', () => {
  let tx: Transaction;
  beforeEach(() => {
    tx = db.getConnection();
  })
  afterEach(() => {
    tx.rollback();
  })
  it('test', async () => {
    const result = await tx.makeSomeDbChanges();
    expect(result).toBe(something);
  })
describe('test suite', () => {
  let tx: Transaction;
  beforeEach(() => {
    tx = db.getConnection();
  })
  afterEach(() => {
    tx.rollback();
  })
  it('test', async () => {
    const result = await tx.makeSomeDbChanges();
    expect(result).toBe(something);
  })


Does Drizzle have a mechanism for getting access to the transaction without the anonymous function route? I'd prefer not to do the following in every test...

describe('test suite', () => {
  it('test', async () => {
    const db = drizzle(conn);
    db.transaction( async (tx) => {
      const result = await tx.makeSomeDbChanges();
      expect(result).toBe(something);
      tx.rollback();
    }); 
  })
}
describe('test suite', () => {
  it('test', async () => {
    const db = drizzle(conn);
    db.transaction( async (tx) => {
      const result = await tx.makeSomeDbChanges();
      expect(result).toBe(something);
      tx.rollback();
    }); 
  })
}

Especially when I have to throw in error handling because
tx.rollback()
tx.rollback()
throws an Error and doesn't simply rollback.

What are others doing for unit testing?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

set up unit testing
Drizzle TeamDTDrizzle Team / help
3y ago
Transactions for DB reset in testing
Drizzle TeamDTDrizzle Team / help
3y ago
Is there any tip for unit testing with drizzle?
Drizzle TeamDTDrizzle Team / help
3y ago
Transactions with postgres
Drizzle TeamDTDrizzle Team / help
3y ago