[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/testing](../../README) / [index](../README) / withDatabase

# Function: withDatabase()

> **withDatabase**(`fn`): () => `Promise`\<`void`\>

Defined in: [testing/src/withDatabase.ts:18](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/testing/src/withDatabase.ts#L18)

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

```ts
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.
}));
```
