# @probitas/builder > Version: 0.5.2 Builder module for creating scenario definitions with a fluent, type-safe API. This package provides the {@linkcode scenario} fn function that returns a builder for constructing scenario definitions. The builder uses method chaining to define resources, setup functions, and test steps with full TypeScript type inference. ## Links - [GitHub Repository](https://github.com/probitas-test/probitas) - [@probitas/probitas](https://jsr.io/@probitas/probitas) - Main package (recommended for most users) ## Related Packages | Package | Description | |---------|-------------| | [@probitas/core](https://jsr.io/@probitas/core) | Core type definitions used by this builder | | [@probitas/runner](https://jsr.io/@probitas/runner) | Executes scenarios built with this package | ## Key Features - **Fluent API**: Chain methods naturally to build complex scenarios - **Type-safe context**: Each step receives typed access to previous step results - **Resource management**: Register resources with automatic lifecycle handling - **Setup/Cleanup**: Define setup functions with automatic cleanup support - **Configurable defaults**: Override timeout and retry settings at any level ## Core Exports - {@linkcode scenario} - Factory function to create a new scenario builder - {@linkcode StepContext} - Type representing the context passed to step functions - {@linkcode StepFunction} - Type signature for step functions - {@linkcode SetupFunction} - Type signature for setup functions - {@linkcode ResourceFunction} - Type signature for resource fn functions - {@linkcode BuilderScenarioOptions} - Partial options for scenario configuration - {@linkcode BuilderStepOptions} - Partial options for step configuration - {@linkcode DEFAULT_SCENARIO_OPTIONS} - Default values for scenario options - {@linkcode DEFAULT_STEP_OPTIONS} - Default values for step options ## Functions ### `scenario` ```typescript function scenario(name: string, options?: ScenarioOptions): ScenarioBuilderInit ``` Create a new scenario builder with a fluent API. This is the primary entry point for defining scenarios. It returns a builder that provides a type-safe, chainable API for constructing test scenarios. **Parameters:** - `name`: `string` — - Human-readable scenario name (shown in test reports) - `options`: `ScenarioOptions` (optional) — - Optional configuration for tags, timeouts, and retry behavior **Returns:** `ScenarioBuilderInit` New {@linkcode ScenarioBuilderInit} instance ready for chaining **Example:** Basic scenario with type-safe step chaining ```ts import { scenario } from "@probitas/builder"; // Mock user service for example const createUser = (_data: { email: string }) => Promise.resolve({ id: "user-123" }); const verifyEmail = (_userId: string) => Promise.resolve(); export default scenario("User Registration") .step("Create user", async () => { const user = await createUser({ email: "test@example.com" }); return { userId: user.id }; }) .step("Verify email", async (ctx) => { // ctx.previous is typed as { userId: string } await verifyEmail(ctx.previous.userId); }) .build(); ``` Scenario with tags for filtering ```ts import { scenario } from "@probitas/builder"; scenario("Payment Integration", { tags: ["integration", "payment", "slow"], stepOptions: { timeout: 60000 } // 1 minute timeout for all steps }) .step("Process payment", async () => { return { success: true }; }) .build(); // Run with: probitas run -s "tag:payment" ``` Scenario with resources and setup ```ts import { scenario } from "@probitas/builder"; // Mock Database for example const Database = { connect: () => Promise.resolve({ rollback: () => {}, insert: (_data: { id: number }) => {}, query: (_sql: string) => [{ id: 1 }], }), }; scenario("Database Test") .resource("db", async () => await Database.connect()) .setup((ctx) => { // Run migrations return () => ctx.resources.db.rollback(); }) .step("Insert data", (ctx) => ctx.resources.db.insert({ id: 1 })) .step("Query data", (ctx) => ctx.resources.db.query("SELECT * FROM users")) .build(); ``` --- ## Types ### `BuilderStepContext` ```typescript type BuilderStepContext
= Record ) => unknown
```
Function signature for step execution.
A step function receives the execution context and returns a value
(sync or async) that becomes available to subsequent steps.
---
### `BuilderStepDefinition`
```typescript
type BuilderStepDefinition