Skip to content

basalt / testing/src / withTenant

Function: withTenant() ​

> withTenant<T>(tenancy, id, fn, options?): Promise<T>

Defined in: testing/src/tenant.ts:54

Provisions a tenant, runs fn inside its context, and removes it afterwards — including when fn throws, which is the case that matters. A failing test that leaves its tenant behind makes the next run fail for a different reason, and that is a bad hour.

ts
await withTenant(tenancy, 'acme', async () => {
  await request('/matters')   // runs with ctx().tenant === acme
})

Any leftover tenant of the same id is destroyed first, with force: a previous run may have died between creating the record and provisioning the schema, and refusing to clean that up would mean the suite can never recover on its own.

Type Parameters ​

T ​

T

Parameters ​

tenancy ​

TenantLifecycle

id ​

string

fn ​

() => T | Promise<T>

options? ​

WithTenantOptions = {}

Returns ​

Promise<T>

Released under the MIT License.