@scenarist/express-adapter 0.1.12 → 0.1.14

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.
package/README.md CHANGED
@@ -182,54 +182,63 @@ export const scenarios = {
182
182
  ### 2. Create Scenarist Instance
183
183
 
184
184
  ```typescript
185
- // test/setup.ts
185
+ // src/app.ts
186
+ import express from "express";
186
187
  import { createScenarist } from "@scenarist/express-adapter";
187
188
  import { scenarios } from "./scenarios";
188
189
 
189
- export const scenarist = createScenarist({
190
- enabled: process.env.NODE_ENV === "test",
191
- scenarios, // All scenarios registered upfront
192
- strictMode: false,
193
- });
194
- ```
195
-
196
- ### 3. Add to Express App
197
-
198
- ```typescript
199
- // src/app.ts
200
- import express from "express";
201
- import { scenarist } from "../test/setup";
190
+ // Factory function for Express app setup
191
+ export const createApp = () => {
192
+ const app = express();
193
+ app.use(express.json());
202
194
 
203
- const app = express();
204
- app.use(express.json());
195
+ // Create Scenarist instance (synchronous)
196
+ const scenarist = createScenarist({
197
+ enabled: process.env.NODE_ENV === "test",
198
+ scenarios, // All scenarios registered upfront
199
+ strictMode: false,
200
+ });
205
201
 
206
- // Add Scenarist middleware (includes everything)
207
- if (process.env.NODE_ENV === "test") {
208
- app.use(scenarist.middleware);
209
- }
202
+ // Add Scenarist middleware (only if enabled)
203
+ if (scenarist) {
204
+ app.use(scenarist.middleware);
205
+ }
210
206
 
211
- // Your application routes
212
- app.get("/api/user", async (req, res) => {
213
- const response = await fetch("https://api.example.com/user");
214
- const user = await response.json();
215
- res.json(user);
216
- });
207
+ // Your application routes
208
+ app.get("/api/user", async (req, res) => {
209
+ const response = await fetch("https://api.example.com/user");
210
+ const user = await response.json();
211
+ res.json(user);
212
+ });
217
213
 
218
- export { app };
214
+ return { app, scenarist };
215
+ };
219
216
  ```
220
217
 
221
- ### 4. Use in Tests
218
+ ### 3. Use in Tests
222
219
 
223
220
  ```typescript
224
221
  // test/api.test.ts
225
222
  import { describe, it, expect, beforeAll, afterAll } from "vitest";
226
223
  import request from "supertest";
227
- import { app } from "../src/app";
228
- import { scenarist } from "./setup";
224
+ import { createApp } from "../src/app";
225
+
226
+ // Factory function for test setup - no let variables
227
+ const createTestSetup = () => {
228
+ const { app, scenarist } = createApp();
229
+ return { app, scenarist };
230
+ };
229
231
 
230
232
  describe("User API", () => {
231
- beforeAll(() => scenarist.start());
232
- afterAll(() => scenarist.stop());
233
+ const { app, scenarist } = createTestSetup();
234
+
235
+ beforeAll(() => {
236
+ scenarist?.start();
237
+ });
238
+
239
+ afterAll(async () => {
240
+ await scenarist?.stop();
241
+ });
233
242
 
234
243
  it("should return admin user", async () => {
235
244
  // Set scenario for this test
@@ -255,6 +264,8 @@ describe("User API", () => {
255
264
 
256
265
  Creates a Scenarist instance with everything wired automatically.
257
266
 
267
+ **Note:** This function is **synchronous** and returns the instance directly (or `undefined` in production).
268
+
258
269
  **Parameters:**
259
270
 
260
271
  ```typescript
@@ -277,6 +288,12 @@ type ExpressAdapterOptions<T extends ScenaristScenarios> = {
277
288
 
278
289
  **Returns:**
279
290
 
291
+ ```typescript
292
+ Promise<ExpressScenarist<T> | undefined>;
293
+ ```
294
+
295
+ Where `ExpressScenarist<T>` is:
296
+
280
297
  ```typescript
281
298
  type ExpressScenarist<T extends ScenaristScenarios> = {
282
299
  config: ScenaristConfig; // Resolved configuration (endpoints, headers, etc.)
@@ -310,10 +327,13 @@ const scenarist = createScenarist({
310
327
  strictMode: false,
311
328
  });
312
329
 
313
- app.use(scenarist.middleware);
330
+ // Only apply middleware if scenarist is defined (not production)
331
+ if (scenarist) {
332
+ app.use(scenarist.middleware);
314
333
 
315
- beforeAll(() => scenarist.start());
316
- afterAll(() => scenarist.stop());
334
+ beforeAll(() => scenarist.start());
335
+ afterAll(() => scenarist.stop());
336
+ }
317
337
  ```
318
338
 
319
339
  ### Scenario Endpoints
@@ -651,14 +671,18 @@ export const scenarios = {
651
671
  // setup.ts - create scenarist with type parameter
652
672
  import { scenarios } from "./scenarios";
653
673
 
654
- export const scenarist = createScenarist({
655
- enabled: true,
656
- scenarios, // ✅ Autocomplete + type-checked!
657
- });
674
+ export const createTestSetup = () => {
675
+ const scenarist = createScenarist({
676
+ enabled: true,
677
+ scenarios, // ✅ Autocomplete + type-checked!
678
+ });
679
+ return scenarist;
680
+ };
658
681
 
659
682
  // test.ts - type-safe scenario switching
660
- scenarist.switchScenario("test-123", "success"); // ✅ Autocomplete works!
661
- scenarist.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
683
+ const scenarist = createTestSetup();
684
+ scenarist?.switchScenario("test-123", "success"); // ✅ Autocomplete works!
685
+ scenarist?.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
662
686
 
663
687
  await request(app)
664
688
  .post(scenarist.config.endpoints.setScenario)
@@ -1,8 +1,11 @@
1
- import type { ScenaristScenarios } from "@scenarist/core";
2
- export type { ExpressAdapterOptions, ExpressScenarist } from "./impl.js";
1
+ export type { ExpressAdapterOptions, ExpressScenarist, } from "./setup-scenarist.js";
3
2
  /**
4
3
  * Production-only entry point that returns undefined without loading any dependencies.
5
- * This file has ZERO imports to guarantee tree-shaking.
4
+ *
5
+ * This file provides a safe stub implementation for createScenarist to prevent
6
+ * MSW and test code from being included in production bundles. Conditional exports
7
+ * ensure this file is used when NODE_ENV=production, achieving 100% tree-shaking
8
+ * of test infrastructure.
6
9
  */
7
- export declare const createScenarist: <T extends ScenaristScenarios>(_options: import("./impl.js").ExpressAdapterOptions<T>) => Promise<import("./impl.js").ExpressScenarist<T> | undefined>;
10
+ export declare const createScenarist: (_options: import("./setup-scenarist.js").ExpressAdapterOptions) => import("./setup-scenarist.js").ExpressScenarist | undefined;
8
11
  //# sourceMappingURL=production.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/setup/production.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzE;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,kBAAkB,EAChE,UAAU,OAAO,WAAW,EAAE,qBAAqB,CAAC,CAAC,CAAC,KACrD,OAAO,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,CAE7D,CAAC"}
1
+ {"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/setup/production.ts"],"names":[],"mappings":"AACA,YAAY,EACV,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,sBAAsB,CAAC;AAE9B;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,OAAO,sBAAsB,EAAE,qBAAqB,KAC7D,OAAO,sBAAsB,EAAE,gBAAgB,GAAG,SAEpD,CAAC"}
@@ -1,7 +1,11 @@
1
1
  /**
2
2
  * Production-only entry point that returns undefined without loading any dependencies.
3
- * This file has ZERO imports to guarantee tree-shaking.
3
+ *
4
+ * This file provides a safe stub implementation for createScenarist to prevent
5
+ * MSW and test code from being included in production bundles. Conditional exports
6
+ * ensure this file is used when NODE_ENV=production, achieving 100% tree-shaking
7
+ * of test infrastructure.
4
8
  */
5
- export const createScenarist = async (_options) => {
9
+ export const createScenarist = (_options) => {
6
10
  return undefined;
7
11
  };
@@ -1,29 +1,32 @@
1
- import type { ScenaristScenarios } from "@scenarist/core";
2
1
  export type { ExpressAdapterOptions, ExpressScenarist } from "./impl.js";
3
2
  /**
4
3
  * Create a Scenarist instance for Express.
5
4
  *
6
- * In production (NODE_ENV=production), this returns undefined to enable
7
- * tree-shaking of all Scenarist code. In development/test environments,
8
- * it returns a fully-functional ExpressScenarist instance.
5
+ * Production tree-shaking is handled via conditional exports in package.json:
6
+ * - Development/test: Uses this file (imports impl.js with full MSW setup)
7
+ * - Production builds: Uses production.js (returns undefined, zero imports)
9
8
  *
10
- * This pattern uses dynamic import to enable tree-shaking: the impl.js module
11
- * is only loaded when needed (non-production). Combined with sideEffects: false,
12
- * bundlers can eliminate the entire impl.js module from production builds.
9
+ * This ensures all test code is eliminated from production bundles.
13
10
  *
14
11
  * @example
15
12
  * ```typescript
16
- * const scenarist = await createScenarist({
13
+ * // src/app.ts
14
+ * import { createScenarist } from '@scenarist/express-adapter';
15
+ * import { scenarios } from './scenarios';
16
+ *
17
+ * export const scenarist = createScenarist({
17
18
  * enabled: true,
18
19
  * scenarios,
19
20
  * });
20
21
  *
21
22
  * if (scenarist) {
22
23
  * app.use(scenarist.middleware);
23
- * beforeAll(() => scenarist.start());
24
- * afterAll(() => scenarist.stop());
25
24
  * }
25
+ *
26
+ * // tests/setup.ts
27
+ * beforeAll(() => scenarist?.start());
28
+ * afterAll(() => scenarist?.stop());
26
29
  * ```
27
30
  */
28
- export declare const createScenarist: <T extends ScenaristScenarios>(options: import("./impl.js").ExpressAdapterOptions<T>) => Promise<import("./impl.js").ExpressScenarist<T> | undefined>;
31
+ export { createScenaristImpl as createScenarist } from "./impl.js";
29
32
  //# sourceMappingURL=setup-scenarist.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setup-scenarist.d.ts","sourceRoot":"","sources":["../../src/setup/setup-scenarist.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,kBAAkB,EAChE,SAAS,OAAO,WAAW,EAAE,qBAAqB,CAAC,CAAC,CAAC,KACpD,OAAO,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,CAU7D,CAAC"}
1
+ {"version":3,"file":"setup-scenarist.d.ts","sourceRoot":"","sources":["../../src/setup/setup-scenarist.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,EAAE,mBAAmB,IAAI,eAAe,EAAE,MAAM,WAAW,CAAC"}
@@ -1,35 +1,30 @@
1
1
  /**
2
2
  * Create a Scenarist instance for Express.
3
3
  *
4
- * In production (NODE_ENV=production), this returns undefined to enable
5
- * tree-shaking of all Scenarist code. In development/test environments,
6
- * it returns a fully-functional ExpressScenarist instance.
4
+ * Production tree-shaking is handled via conditional exports in package.json:
5
+ * - Development/test: Uses this file (imports impl.js with full MSW setup)
6
+ * - Production builds: Uses production.js (returns undefined, zero imports)
7
7
  *
8
- * This pattern uses dynamic import to enable tree-shaking: the impl.js module
9
- * is only loaded when needed (non-production). Combined with sideEffects: false,
10
- * bundlers can eliminate the entire impl.js module from production builds.
8
+ * This ensures all test code is eliminated from production bundles.
11
9
  *
12
10
  * @example
13
11
  * ```typescript
14
- * const scenarist = await createScenarist({
12
+ * // src/app.ts
13
+ * import { createScenarist } from '@scenarist/express-adapter';
14
+ * import { scenarios } from './scenarios';
15
+ *
16
+ * export const scenarist = createScenarist({
15
17
  * enabled: true,
16
18
  * scenarios,
17
19
  * });
18
20
  *
19
21
  * if (scenarist) {
20
22
  * app.use(scenarist.middleware);
21
- * beforeAll(() => scenarist.start());
22
- * afterAll(() => scenarist.stop());
23
23
  * }
24
+ *
25
+ * // tests/setup.ts
26
+ * beforeAll(() => scenarist?.start());
27
+ * afterAll(() => scenarist?.stop());
24
28
  * ```
25
29
  */
26
- export const createScenarist = async (options) => {
27
- // In production, return undefined without loading impl.js
28
- // Dynamic import below is never executed, enabling tree-shaking
29
- if (process.env.NODE_ENV === "production") {
30
- return undefined;
31
- }
32
- // In non-production, dynamically import and create instance
33
- const { createScenaristImpl } = await import("./impl.js");
34
- return createScenaristImpl(options);
35
- };
30
+ export { createScenaristImpl as createScenarist } from "./impl.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenarist/express-adapter",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Express middleware adapter for Scenarist",
5
5
  "author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
6
6
  "license": "MIT",
@@ -48,8 +48,8 @@
48
48
  ],
49
49
  "dependencies": {
50
50
  "zod": "^4.1.13",
51
- "@scenarist/msw-adapter": "0.1.2",
52
- "@scenarist/core": "0.1.2"
51
+ "@scenarist/core": "0.1.2",
52
+ "@scenarist/msw-adapter": "0.1.2"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "express": "^4.18.0 || ^5.0.0",