Skip to main content
zerotal

Documentation


Documentation / @zerotal/testing / index / withDatabase

Function: withDatabase()

withDatabase(fn): () => Promise<void>

Defined in: testing/src/withDatabase.ts:18

Wrap a test function in a database transaction that always rolls back. Every query inside the callback uses the transaction, so changes are invisible to other connections and never reach the actual database.

Use with bun:test's it():

Parameters

fn

() => Promise<void>

Returns

() => Promise<void>

Example

it('creates a user', withDatabase(async () => {
  const user = await User.create({ name: 'Alice', email: 'alice@example.com' });
  expect(user.id).toBeDefined();
  await assertDatabaseHas('users', { email: 'alice@example.com' });
  // Rolls back after this function returns — no cleanup needed.
}));