testing
Classes
TestTenancyModule
Defined in: src/testing/test-tenancy.module.ts:22
A lightweight test module that provides TenancyContext and TenancyService without the middleware, guard, or module options required by the production TenancyModule.
Usage in tests:
const module = await Test.createTestingModule({
imports: [TestTenancyModule.register()],
providers: [MyService],
}).compile();
const service = module.get(MyService);
const result = await withTenant('tenant-1', () => service.findAll());Constructors
Constructor
new TestTenancyModule(): TestTenancyModule;Returns
Methods
register()
static register(): DynamicModule;Defined in: src/testing/test-tenancy.module.ts:23
Returns
DynamicModule
Interfaces
IsolationTestOptions
Defined in: src/testing/expect-tenant-isolation.ts:3
Properties
tenantIdField?
optional tenantIdField?: string;Defined in: src/testing/expect-tenant-isolation.ts:5
The field name that holds the tenant ID.
Default
'tenant_id'Functions
expectTenantIsolation()
function expectTenantIsolation(
prismaModel,
tenantA,
tenantB,
options?): Promise<void>;Defined in: src/testing/expect-tenant-isolation.ts:25
Asserts that a Prisma model enforces tenant isolation between two tenants.
Executes findMany() concurrently as both tenants and verifies that no rows from tenant A appear in tenant B's results, and vice versa.
Usage in E2E tests:
await expectTenantIsolation(prisma.user, 'tenant-a-uuid', 'tenant-b-uuid');Parameters
| Parameter | Type | Description |
|---|---|---|
prismaModel | { findMany: (args?) => Promise<Record<string, unknown>[]>; } | A Prisma model delegate with a findMany method |
prismaModel.findMany | (args?) => Promise<Record<string, unknown>[]> | - |
tenantA | string | First tenant ID |
tenantB | string | Second tenant ID |
options? | IsolationTestOptions | Optional configuration |
Returns
Promise<void>
Throws
Error if tenant isolation is violated
withTenant()
function withTenant<T>(
tenantId,
callback,
context?): Promise<T>;Defined in: src/testing/with-tenant.ts:26
Runs a callback within a tenant context, handling async/await properly.
Simplifies the common test pattern:
// Before (verbose)
await new Promise<void>((resolve) => {
context.run('tenant-1', async () => {
const result = await service.findAll();
expect(result).toHaveLength(3);
resolve();
});
});
// After (with helper)
const result = await withTenant('tenant-1', () => service.findAll());
expect(result).toHaveLength(3);Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
tenantId | string | The tenant ID to set in context |
callback | () => T | Promise<T> | The async function to execute within the tenant context |
context? | TenancyContext | Optional TenancyContext instance (uses a new instance by default; works because AsyncLocalStorage is static) |
Returns
Promise<T>