Connectum API Reference / @connectum/auth / testing / withAuthContext
Function: withAuthContext()
withAuthContext<
T>(context,fn):Promise<T>
Defined in: packages/auth/src/testing/with-context.ts:31
Run a function with a pre-set AuthContext.
Sets the provided AuthContext in AsyncLocalStorage for the duration of the callback. Useful for testing handlers that call getAuthContext().
Type Parameters
T
T
Parameters
context
Auth context to set
fn
() => T | Promise<T>
Function to execute within the context
Returns
Promise<T>
Return value of fn
Example
typescript
import { withAuthContext, createMockAuthContext } from '@connectum/auth/testing';
import { getAuthContext } from '@connectum/auth';
await withAuthContext(createMockAuthContext({ subject: 'test-user' }), async () => {
const ctx = getAuthContext();
assert.strictEqual(ctx?.subject, 'test-user');
});