@glubean/sdk 0.1.39 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/configure.d.ts +4 -21
  2. package/dist/configure.d.ts.map +1 -1
  3. package/dist/configure.js +19 -145
  4. package/dist/configure.js.map +1 -1
  5. package/dist/contract-core.d.ts +98 -0
  6. package/dist/contract-core.d.ts.map +1 -0
  7. package/dist/contract-core.js +749 -0
  8. package/dist/contract-core.js.map +1 -0
  9. package/dist/contract-http/adapter.d.ts +52 -0
  10. package/dist/contract-http/adapter.d.ts.map +1 -0
  11. package/dist/contract-http/adapter.js +650 -0
  12. package/dist/contract-http/adapter.js.map +1 -0
  13. package/dist/contract-http/factory.d.ts +32 -0
  14. package/dist/contract-http/factory.d.ts.map +1 -0
  15. package/dist/contract-http/factory.js +83 -0
  16. package/dist/contract-http/factory.js.map +1 -0
  17. package/dist/contract-http/flow-helpers.d.ts +12 -0
  18. package/dist/contract-http/flow-helpers.d.ts.map +1 -0
  19. package/dist/contract-http/flow-helpers.js +34 -0
  20. package/dist/contract-http/flow-helpers.js.map +1 -0
  21. package/dist/contract-http/index.d.ts +16 -0
  22. package/dist/contract-http/index.d.ts.map +1 -0
  23. package/dist/contract-http/index.js +15 -0
  24. package/dist/contract-http/index.js.map +1 -0
  25. package/dist/contract-http/markdown.d.ts +10 -0
  26. package/dist/contract-http/markdown.d.ts.map +1 -0
  27. package/dist/contract-http/markdown.js +21 -0
  28. package/dist/contract-http/markdown.js.map +1 -0
  29. package/dist/contract-http/openapi.d.ts +15 -0
  30. package/dist/contract-http/openapi.d.ts.map +1 -0
  31. package/dist/contract-http/openapi.js +38 -0
  32. package/dist/contract-http/openapi.js.map +1 -0
  33. package/dist/contract-http/types.d.ts +252 -0
  34. package/dist/contract-http/types.d.ts.map +1 -0
  35. package/dist/contract-http/types.js +13 -0
  36. package/dist/contract-http/types.js.map +1 -0
  37. package/dist/contract-types.d.ts +420 -467
  38. package/dist/contract-types.d.ts.map +1 -1
  39. package/dist/contract-types.js +16 -4
  40. package/dist/contract-types.js.map +1 -1
  41. package/dist/each-builder.d.ts +244 -0
  42. package/dist/each-builder.d.ts.map +1 -0
  43. package/dist/each-builder.js +268 -0
  44. package/dist/each-builder.js.map +1 -0
  45. package/dist/index.d.ts +30 -513
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +33 -826
  48. package/dist/index.js.map +1 -1
  49. package/dist/internal.d.ts +1 -0
  50. package/dist/internal.d.ts.map +1 -1
  51. package/dist/internal.js +1 -0
  52. package/dist/internal.js.map +1 -1
  53. package/dist/runtime-carrier.d.ts +142 -0
  54. package/dist/runtime-carrier.d.ts.map +1 -0
  55. package/dist/runtime-carrier.js +148 -0
  56. package/dist/runtime-carrier.js.map +1 -0
  57. package/dist/session.d.ts.map +1 -1
  58. package/dist/session.js +2 -1
  59. package/dist/session.js.map +1 -1
  60. package/dist/test-builder.d.ts +249 -0
  61. package/dist/test-builder.d.ts.map +1 -0
  62. package/dist/test-builder.js +265 -0
  63. package/dist/test-builder.js.map +1 -0
  64. package/dist/test-extend.d.ts +59 -0
  65. package/dist/test-extend.d.ts.map +1 -0
  66. package/dist/test-extend.js +111 -0
  67. package/dist/test-extend.js.map +1 -0
  68. package/dist/test-utils.d.ts +39 -0
  69. package/dist/test-utils.d.ts.map +1 -0
  70. package/dist/test-utils.js +91 -0
  71. package/dist/test-utils.js.map +1 -0
  72. package/dist/types.d.ts +41 -122
  73. package/dist/types.d.ts.map +1 -1
  74. package/package.json +1 -1
  75. package/dist/contract.d.ts +0 -64
  76. package/dist/contract.d.ts.map +0 -1
  77. package/dist/contract.js +0 -793
  78. package/dist/contract.js.map +0 -1
@@ -0,0 +1,249 @@
1
+ /**
2
+ * @module test-builder
3
+ *
4
+ * Fluent builder for multi-step tests (`TestBuilder`).
5
+ *
6
+ * Entry point: `test("id")` (no callback) returns a `TestBuilder`.
7
+ * Chain `.setup()` → `.step()` → `.teardown()` to define the test workflow.
8
+ * State flows between steps via the return value of each step function.
9
+ * `.build()` is optional — the builder auto-registers via microtask.
10
+ */
11
+ import type { ExtensionFn, StepMeta, Test, TestContext, TestMeta } from "./types.js";
12
+ /**
13
+ * Builder class for creating tests with a fluent API.
14
+ *
15
+ * @template S The state type for multi-step tests
16
+ * @template Ctx The context type (defaults to TestContext; augmented by test.extend())
17
+ *
18
+ * @example Simple test (quick mode)
19
+ * ```ts
20
+ * export const login = test("login", async (ctx) => {
21
+ * ctx.assert(true, "works");
22
+ * });
23
+ * ```
24
+ *
25
+ * @example Multi-step test (builder mode)
26
+ * ```ts
27
+ * export const checkout = test("checkout")
28
+ * .meta({ tags: ["e2e"] })
29
+ * .setup(async (ctx) => ({ cart: await createCart() }))
30
+ * .step("Add to cart", async (ctx, state) => {
31
+ * await addItem(state.cart, "item-1");
32
+ * return state;
33
+ * })
34
+ * .step("Checkout", async (ctx, state) => {
35
+ * await checkout(state.cart);
36
+ * return state;
37
+ * })
38
+ * .teardown(async (ctx, state) => {
39
+ * await cleanup(state.cart);
40
+ * })
41
+ * .build();
42
+ * ```
43
+ */
44
+ export declare class TestBuilder<S = unknown, Ctx extends TestContext = TestContext> {
45
+ private _meta;
46
+ private _setup?;
47
+ private _teardown?;
48
+ private _steps;
49
+ private _built;
50
+ _fixtures?: Record<string, ExtensionFn<any>>;
51
+ /**
52
+ * Marker property so the runner can detect un-built TestBuilder exports
53
+ * without importing the SDK. The runner checks this string to auto-build.
54
+ */
55
+ readonly __glubean_type: "builder";
56
+ constructor(id: string, fixtures?: Record<string, ExtensionFn<any>>);
57
+ /**
58
+ * Set additional metadata for the test.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * test("my-test")
63
+ * .meta({ tags: ["smoke"], description: "A smoke test" })
64
+ * .step(...)
65
+ * ```
66
+ */
67
+ meta(meta: Omit<TestMeta, "id">): TestBuilder<S, Ctx>;
68
+ /**
69
+ * Mark this test as focused.
70
+ *
71
+ * Focused tests are intended for local debugging sessions. When any tests in
72
+ * a run are marked as `only`, non-focused tests may be excluded by discovery
73
+ * tooling/orchestrators. If `skip` is also set on the same test, `skip`
74
+ * still wins during run selection.
75
+ */
76
+ only(): TestBuilder<S, Ctx>;
77
+ /**
78
+ * Mark this test as skipped.
79
+ *
80
+ * Skip takes precedence over `only` when both are present.
81
+ */
82
+ skip(): TestBuilder<S, Ctx>;
83
+ /**
84
+ * Set the setup function that runs before all steps.
85
+ * The returned state is passed to all steps and teardown.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * test("auth")
90
+ * .setup(async (ctx) => {
91
+ * const baseUrl = ctx.vars.require("BASE_URL");
92
+ * const apiKey = ctx.secrets.require("API_KEY");
93
+ * const { token } = await ctx.http.post(`${baseUrl}/auth/token`, {
94
+ * headers: { "X-API-Key": apiKey },
95
+ * }).json();
96
+ * return { token };
97
+ * })
98
+ * .step("verify", async (ctx, { token }) => { ... })
99
+ * ```
100
+ */
101
+ setup<NewS>(fn: (ctx: Ctx) => Promise<NewS>): TestBuilder<NewS, Ctx>;
102
+ /**
103
+ * Set the teardown function that runs after all steps (even on failure).
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * test("db-test")
108
+ * .setup(async (ctx) => ({ conn: await connect() }))
109
+ * .step(...)
110
+ * .teardown(async (ctx, { conn }) => {
111
+ * await conn.close();
112
+ * })
113
+ * ```
114
+ */
115
+ teardown(fn: (ctx: Ctx, state: S) => Promise<void>): TestBuilder<S, Ctx>;
116
+ /**
117
+ * Add a step that does not return state (void).
118
+ * The state type is preserved for subsequent steps.
119
+ *
120
+ * @param name Step name (displayed in reports)
121
+ * @param fn Step function that performs assertions/side-effects without returning state
122
+ */
123
+ step(name: string, fn: (ctx: Ctx, state: S) => Promise<void>): TestBuilder<S, Ctx>;
124
+ /**
125
+ * Add a step that returns new state, replacing the current state type.
126
+ *
127
+ * The returned value becomes the `state` argument for subsequent steps.
128
+ * This enables fully type-safe chained steps without needing `.setup()`.
129
+ *
130
+ * @param name Step name (displayed in reports)
131
+ * @param fn Step function receiving context and current state, returning new state
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * test("auth-flow")
136
+ * .step("login", async (ctx) => {
137
+ * const data = await ctx.http.post("/auth/login", { json: creds }).json<{ token: string }>();
138
+ * return { token: data.token };
139
+ * })
140
+ * .step("get profile", async (ctx, { token }) => {
141
+ * // token is inferred as string ✓
142
+ * const profile = await ctx.http.get("/auth/me", {
143
+ * headers: { Authorization: `Bearer ${token}` },
144
+ * }).json<{ name: string }>();
145
+ * return { token, name: profile.name };
146
+ * })
147
+ * ```
148
+ */
149
+ step<NewS>(name: string, fn: (ctx: Ctx, state: S) => Promise<NewS>): TestBuilder<NewS, Ctx>;
150
+ /**
151
+ * Add a step with options (void return).
152
+ */
153
+ step(name: string, options: Omit<StepMeta, "name">, fn: (ctx: Ctx, state: S) => Promise<void>): TestBuilder<S, Ctx>;
154
+ /**
155
+ * Add a step with additional options, returning new state.
156
+ */
157
+ step<NewS>(name: string, options: Omit<StepMeta, "name">, fn: (ctx: Ctx, state: S) => Promise<NewS>): TestBuilder<NewS, Ctx>;
158
+ /**
159
+ * Apply a builder transform function for step composition.
160
+ *
161
+ * Reusable step sequences are just plain functions that take a builder
162
+ * and return a builder. `.use()` applies such a function to the current
163
+ * chain, preserving state flow.
164
+ *
165
+ * @param fn Transform function that receives this builder and returns a (possibly re-typed) builder
166
+ *
167
+ * @example Reusable step sequence
168
+ * ```ts
169
+ * // Define once — just a function
170
+ * const withAuth = (b: TestBuilder<unknown>) => b
171
+ * .step("login", async (ctx) => {
172
+ * const data = await ctx.http.post("/login", { json: creds }).json<{ token: string }>();
173
+ * return { token: data.token };
174
+ * });
175
+ *
176
+ * // Reuse across tests
177
+ * export const testA = test("test-a").use(withAuth).step("act", async (ctx, { token }) => { ... });
178
+ * export const testB = test("test-b").use(withAuth).step("verify", async (ctx, { token }) => { ... });
179
+ * ```
180
+ */
181
+ use<NewS>(fn: (builder: TestBuilder<S, Ctx>) => TestBuilder<NewS, Ctx>): TestBuilder<NewS, Ctx>;
182
+ /**
183
+ * Apply a builder transform and tag all newly added steps with a group ID.
184
+ *
185
+ * Works exactly like `.use()`, but every step added by `fn` is marked with
186
+ * `group` metadata for visual grouping in reports and dashboards.
187
+ *
188
+ * @param id Group identifier (displayed in reports as a section header)
189
+ * @param fn Transform function that adds steps to the builder
190
+ *
191
+ * @example Reusable steps with grouping
192
+ * ```ts
193
+ * const withAuth = (b: TestBuilder<unknown>) => b
194
+ * .step("login", async (ctx) => ({ token: "..." }))
195
+ * .step("verify", async (ctx, { token }) => ({ token, verified: true }));
196
+ *
197
+ * export const checkout = test("checkout")
198
+ * .group("auth", withAuth)
199
+ * .step("pay", async (ctx, { token }) => { ... });
200
+ *
201
+ * // Report output:
202
+ * // checkout
203
+ * // ├─ [auth]
204
+ * // │ ├─ login ✓
205
+ * // │ └─ verify ✓
206
+ * // └─ pay ✓
207
+ * ```
208
+ *
209
+ * @example Inline grouping (no reuse, just organization)
210
+ * ```ts
211
+ * export const e2e = test("e2e")
212
+ * .group("setup", b => b
213
+ * .step("seed db", async (ctx) => ({ dbId: "..." }))
214
+ * .step("create user", async (ctx, { dbId }) => ({ dbId, userId: "..." }))
215
+ * )
216
+ * .step("verify", async (ctx, { dbId, userId }) => { ... });
217
+ * ```
218
+ */
219
+ group<NewS>(id: string, fn: (builder: TestBuilder<S, Ctx>) => TestBuilder<NewS, Ctx>): TestBuilder<NewS, Ctx>;
220
+ /**
221
+ * Finalize and register the test in the global registry.
222
+ * Called automatically via microtask if not explicitly invoked via build().
223
+ * Idempotent — safe to call multiple times.
224
+ * @internal
225
+ */
226
+ private _finalize;
227
+ /**
228
+ * Build and register the test. Returns a plain `Test<S>` object.
229
+ *
230
+ * **Optional** — if omitted, the builder auto-finalizes via microtask
231
+ * after all synchronous chaining completes, and the runner will
232
+ * auto-detect the builder export. Calling `.build()` explicitly is
233
+ * still supported for backward compatibility.
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * // With .build() (explicit — backward compatible)
238
+ * export const myTest = test("my-test")
239
+ * .step("step-1", async (ctx) => { ... })
240
+ * .build();
241
+ *
242
+ * // Without .build() (auto-finalized — recommended)
243
+ * export const myTest = test("my-test")
244
+ * .step("step-1", async (ctx) => { ... });
245
+ * ```
246
+ */
247
+ build(): Test<S>;
248
+ }
249
+ //# sourceMappingURL=test-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-builder.d.ts","sourceRoot":"","sources":["../src/test-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EACV,WAAW,EAIX,QAAQ,EAER,IAAI,EACJ,WAAW,EACX,QAAQ,EACT,MAAM,YAAY,CAAC;AAIpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,WAAW,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,SAAS,WAAW,GAAG,WAAW;IACzE,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,MAAM,CAAC,CAAmB;IAClC,OAAO,CAAC,SAAS,CAAC,CAAsB;IAExC,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,MAAM,CAAS;IAEvB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;IAE7C;;;OAGG;IACH,QAAQ,CAAC,cAAc,EAAG,SAAS,CAAU;gBAG3C,EAAE,EAAE,MAAM,EACV,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;IAU7C;;;;;;;;;OASG;IACH,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,GAAG,CAAC;IAKrD;;;;;;;OAOG;IACH,IAAI,IAAI,WAAW,CAAC,CAAC,EAAE,GAAG,CAAC;IAK3B;;;;OAIG;IACH,IAAI,IAAI,WAAW,CAAC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AAI,CAAC,GACxC,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC;IAmBzB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,GAAG,CAAC,IAAI,EACN,EAAE,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC,GAC3D,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC;IAIzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,KAAK,CAAC,IAAI,EACR,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC,GAC3D,WAAW,CAAC,IAAI,EAAE,GAAG,CAAC;IASzB;;;;;OAKG;IACH,OAAO,CAAC,SAAS;IAmBjB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC;CAYjB"}
@@ -0,0 +1,265 @@
1
+ import { registerTest } from "./internal.js";
2
+ import { toArray } from "./data.js";
3
+ /**
4
+ * Builder class for creating tests with a fluent API.
5
+ *
6
+ * @template S The state type for multi-step tests
7
+ * @template Ctx The context type (defaults to TestContext; augmented by test.extend())
8
+ *
9
+ * @example Simple test (quick mode)
10
+ * ```ts
11
+ * export const login = test("login", async (ctx) => {
12
+ * ctx.assert(true, "works");
13
+ * });
14
+ * ```
15
+ *
16
+ * @example Multi-step test (builder mode)
17
+ * ```ts
18
+ * export const checkout = test("checkout")
19
+ * .meta({ tags: ["e2e"] })
20
+ * .setup(async (ctx) => ({ cart: await createCart() }))
21
+ * .step("Add to cart", async (ctx, state) => {
22
+ * await addItem(state.cart, "item-1");
23
+ * return state;
24
+ * })
25
+ * .step("Checkout", async (ctx, state) => {
26
+ * await checkout(state.cart);
27
+ * return state;
28
+ * })
29
+ * .teardown(async (ctx, state) => {
30
+ * await cleanup(state.cart);
31
+ * })
32
+ * .build();
33
+ * ```
34
+ */
35
+ export class TestBuilder {
36
+ _meta;
37
+ _setup;
38
+ _teardown;
39
+ _steps = [];
40
+ _built = false;
41
+ _fixtures;
42
+ /**
43
+ * Marker property so the runner can detect un-built TestBuilder exports
44
+ * without importing the SDK. The runner checks this string to auto-build.
45
+ */
46
+ __glubean_type = "builder";
47
+ constructor(id, fixtures) {
48
+ this._meta = { id, name: id };
49
+ this._fixtures = fixtures;
50
+ // Auto-finalize (register) after all synchronous chaining completes.
51
+ // Module top-level code is synchronous, so by the time the microtask
52
+ // fires, all .step() / .meta() / .setup() / .teardown() calls are done.
53
+ queueMicrotask(() => this._finalize());
54
+ }
55
+ /**
56
+ * Set additional metadata for the test.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * test("my-test")
61
+ * .meta({ tags: ["smoke"], description: "A smoke test" })
62
+ * .step(...)
63
+ * ```
64
+ */
65
+ meta(meta) {
66
+ this._meta = { ...this._meta, ...meta };
67
+ return this;
68
+ }
69
+ /**
70
+ * Mark this test as focused.
71
+ *
72
+ * Focused tests are intended for local debugging sessions. When any tests in
73
+ * a run are marked as `only`, non-focused tests may be excluded by discovery
74
+ * tooling/orchestrators. If `skip` is also set on the same test, `skip`
75
+ * still wins during run selection.
76
+ */
77
+ only() {
78
+ this._meta = { ...this._meta, only: true };
79
+ return this;
80
+ }
81
+ /**
82
+ * Mark this test as skipped.
83
+ *
84
+ * Skip takes precedence over `only` when both are present.
85
+ */
86
+ skip() {
87
+ this._meta = { ...this._meta, skip: true };
88
+ return this;
89
+ }
90
+ /**
91
+ * Set the setup function that runs before all steps.
92
+ * The returned state is passed to all steps and teardown.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * test("auth")
97
+ * .setup(async (ctx) => {
98
+ * const baseUrl = ctx.vars.require("BASE_URL");
99
+ * const apiKey = ctx.secrets.require("API_KEY");
100
+ * const { token } = await ctx.http.post(`${baseUrl}/auth/token`, {
101
+ * headers: { "X-API-Key": apiKey },
102
+ * }).json();
103
+ * return { token };
104
+ * })
105
+ * .step("verify", async (ctx, { token }) => { ... })
106
+ * ```
107
+ */
108
+ setup(fn) {
109
+ this._setup = fn;
110
+ return this;
111
+ }
112
+ /**
113
+ * Set the teardown function that runs after all steps (even on failure).
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * test("db-test")
118
+ * .setup(async (ctx) => ({ conn: await connect() }))
119
+ * .step(...)
120
+ * .teardown(async (ctx, { conn }) => {
121
+ * await conn.close();
122
+ * })
123
+ * ```
124
+ */
125
+ teardown(fn) {
126
+ this._teardown = fn;
127
+ return this;
128
+ }
129
+ step(name, optionsOrFn, maybeFn) {
130
+ const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
131
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
132
+ this._steps.push({
133
+ meta: { name, ...options },
134
+ fn: fn,
135
+ });
136
+ return this;
137
+ }
138
+ /**
139
+ * Apply a builder transform function for step composition.
140
+ *
141
+ * Reusable step sequences are just plain functions that take a builder
142
+ * and return a builder. `.use()` applies such a function to the current
143
+ * chain, preserving state flow.
144
+ *
145
+ * @param fn Transform function that receives this builder and returns a (possibly re-typed) builder
146
+ *
147
+ * @example Reusable step sequence
148
+ * ```ts
149
+ * // Define once — just a function
150
+ * const withAuth = (b: TestBuilder<unknown>) => b
151
+ * .step("login", async (ctx) => {
152
+ * const data = await ctx.http.post("/login", { json: creds }).json<{ token: string }>();
153
+ * return { token: data.token };
154
+ * });
155
+ *
156
+ * // Reuse across tests
157
+ * export const testA = test("test-a").use(withAuth).step("act", async (ctx, { token }) => { ... });
158
+ * export const testB = test("test-b").use(withAuth).step("verify", async (ctx, { token }) => { ... });
159
+ * ```
160
+ */
161
+ use(fn) {
162
+ return fn(this);
163
+ }
164
+ /**
165
+ * Apply a builder transform and tag all newly added steps with a group ID.
166
+ *
167
+ * Works exactly like `.use()`, but every step added by `fn` is marked with
168
+ * `group` metadata for visual grouping in reports and dashboards.
169
+ *
170
+ * @param id Group identifier (displayed in reports as a section header)
171
+ * @param fn Transform function that adds steps to the builder
172
+ *
173
+ * @example Reusable steps with grouping
174
+ * ```ts
175
+ * const withAuth = (b: TestBuilder<unknown>) => b
176
+ * .step("login", async (ctx) => ({ token: "..." }))
177
+ * .step("verify", async (ctx, { token }) => ({ token, verified: true }));
178
+ *
179
+ * export const checkout = test("checkout")
180
+ * .group("auth", withAuth)
181
+ * .step("pay", async (ctx, { token }) => { ... });
182
+ *
183
+ * // Report output:
184
+ * // checkout
185
+ * // ├─ [auth]
186
+ * // │ ├─ login ✓
187
+ * // │ └─ verify ✓
188
+ * // └─ pay ✓
189
+ * ```
190
+ *
191
+ * @example Inline grouping (no reuse, just organization)
192
+ * ```ts
193
+ * export const e2e = test("e2e")
194
+ * .group("setup", b => b
195
+ * .step("seed db", async (ctx) => ({ dbId: "..." }))
196
+ * .step("create user", async (ctx, { dbId }) => ({ dbId, userId: "..." }))
197
+ * )
198
+ * .step("verify", async (ctx, { dbId, userId }) => { ... });
199
+ * ```
200
+ */
201
+ group(id, fn) {
202
+ const before = this._steps.length;
203
+ const result = fn(this);
204
+ for (let i = before; i < this._steps.length; i++) {
205
+ this._steps[i].meta.group = id;
206
+ }
207
+ return result;
208
+ }
209
+ /**
210
+ * Finalize and register the test in the global registry.
211
+ * Called automatically via microtask if not explicitly invoked via build().
212
+ * Idempotent — safe to call multiple times.
213
+ * @internal
214
+ */
215
+ _finalize() {
216
+ if (this._built)
217
+ return;
218
+ this._built = true;
219
+ registerTest({
220
+ id: this._meta.id,
221
+ name: this._meta.name || this._meta.id,
222
+ type: "steps",
223
+ tags: toArray(this._meta.tags),
224
+ description: this._meta.description,
225
+ steps: this._steps.map((s) => ({
226
+ name: s.meta.name,
227
+ ...(s.meta.group ? { group: s.meta.group } : {}),
228
+ })),
229
+ hasSetup: !!this._setup,
230
+ hasTeardown: !!this._teardown,
231
+ });
232
+ }
233
+ /**
234
+ * Build and register the test. Returns a plain `Test<S>` object.
235
+ *
236
+ * **Optional** — if omitted, the builder auto-finalizes via microtask
237
+ * after all synchronous chaining completes, and the runner will
238
+ * auto-detect the builder export. Calling `.build()` explicitly is
239
+ * still supported for backward compatibility.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * // With .build() (explicit — backward compatible)
244
+ * export const myTest = test("my-test")
245
+ * .step("step-1", async (ctx) => { ... })
246
+ * .build();
247
+ *
248
+ * // Without .build() (auto-finalized — recommended)
249
+ * export const myTest = test("my-test")
250
+ * .step("step-1", async (ctx) => { ... });
251
+ * ```
252
+ */
253
+ build() {
254
+ this._finalize();
255
+ return {
256
+ meta: this._meta,
257
+ type: "steps",
258
+ setup: this._setup,
259
+ teardown: this._teardown,
260
+ steps: this._steps,
261
+ ...(this._fixtures ? { fixtures: this._fixtures } : {}),
262
+ };
263
+ }
264
+ }
265
+ //# sourceMappingURL=test-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-builder.js","sourceRoot":"","sources":["../src/test-builder.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,OAAO,WAAW;IACd,KAAK,CAAW;IAChB,MAAM,CAAoB;IAC1B,SAAS,CAAuB;IAEhC,MAAM,GAA0B,EAAE,CAAC;IACnC,MAAM,GAAG,KAAK,CAAC;IAEvB,SAAS,CAAoC;IAE7C;;;OAGG;IACM,cAAc,GAAG,SAAkB,CAAC;IAE7C,YACE,EAAU,EACV,QAA2C;QAE3C,IAAI,CAAC,KAAK,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QAC9B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,qEAAqE;QACrE,qEAAqE;QACrE,wEAAwE;QACxE,cAAc,CAAC,GAAG,EAAE,CAA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,CAAC,CAAC,CAAC,EAAE,CAAC;aACjD,CAAC,CAAC;YACH,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM;YACvB,WAAW,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS;SAC9B,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK;QACH,IAAI,CAAC,SAAS,EAAE,CAAC;QAEjB,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,KAAK,EAAE,IAAI,CAAC,MAA6B;YACzC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACxD,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @module test-extend
3
+ *
4
+ * Fixture extension system for augmenting `TestContext` (`test.extend()`).
5
+ *
6
+ * `test.extend(fixtures)` returns an `ExtendedTest` function whose `ctx`
7
+ * includes the resolved fixture properties alongside the base `TestContext`.
8
+ * Supports chained `.extend()`, `.each()`, and `.pick()`.
9
+ * Also exports the `ExtendedTest` interface and `EachOptions`.
10
+ */
11
+ import type { ExtensionFn, ResolveExtensions, Test, TestContext, TestMeta } from "./types.js";
12
+ import { TestBuilder } from "./test-builder.js";
13
+ import { EachBuilder } from "./each-builder.js";
14
+ /** Keys that cannot be used as extension names (they shadow core TestContext). */
15
+ export declare const EXTEND_RESERVED_KEYS: Set<string>;
16
+ /**
17
+ * An extended `test` function created by `test.extend()`.
18
+ *
19
+ * Behaves identically to the base `test()` but augments the context type
20
+ * with fixture properties. Supports quick mode, builder mode, `.each()`,
21
+ * `.pick()`, and chained `.extend()`.
22
+ *
23
+ * @template Ctx The augmented context type (TestContext & extensions)
24
+ */
25
+ export interface ExtendedTest<Ctx extends TestContext> {
26
+ /** Quick mode: single-function test with augmented context. */
27
+ (idOrMeta: string | TestMeta, fn: (ctx: Ctx) => Promise<void>): Test;
28
+ /** Builder mode: multi-step test with augmented context. */
29
+ <S = unknown>(idOrMeta: string | TestMeta): TestBuilder<S, Ctx>;
30
+ /**
31
+ * Chain another set of extensions on top of the current ones.
32
+ * The returned test function has `Ctx & NewExtensions` as its context type.
33
+ */
34
+ extend<E extends Record<string, ExtensionFn<unknown>>>(extensions: E): ExtendedTest<Ctx & ResolveExtensions<E>>;
35
+ /** Data-driven tests with augmented context. */
36
+ each<T extends Record<string, unknown>>(table: readonly T[], options?: EachOptions): {
37
+ (idOrMeta: string | TestMeta, fn: (ctx: Ctx, data: T) => Promise<void>): Test[];
38
+ (idOrMeta: string | TestMeta): EachBuilder<unknown, T, Ctx>;
39
+ };
40
+ /** Example-selection tests with augmented context. */
41
+ pick<T extends Record<string, unknown>>(examples: Record<string, T>, count?: number): {
42
+ (idOrMeta: string | TestMeta, fn: (ctx: Ctx, data: T & {
43
+ _pick: string;
44
+ }) => Promise<void>): Test[];
45
+ (idOrMeta: string | TestMeta): EachBuilder<unknown, T & {
46
+ _pick: string;
47
+ }, Ctx>;
48
+ };
49
+ }
50
+ /** @deprecated Use `parallel` in TestMeta instead. */
51
+ export interface EachOptions {
52
+ parallel?: boolean;
53
+ }
54
+ /**
55
+ * Create an extended test function with fixture definitions.
56
+ * @internal
57
+ */
58
+ export declare function createExtendedTest<Ctx extends TestContext>(allFixtures: Record<string, ExtensionFn<any>>): ExtendedTest<Ctx>;
59
+ //# sourceMappingURL=test-extend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-extend.d.ts","sourceRoot":"","sources":["../src/test-extend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EACV,WAAW,EACX,iBAAiB,EAEjB,IAAI,EACJ,WAAW,EACX,QAAQ,EACT,MAAM,YAAY,CAAC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