@scenarist/express-adapter 0.1.12 → 0.1.13

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 (2) hide show
  1. package/README.md +86 -50
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -182,56 +182,69 @@ 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
+ // Use async factory pattern for Express apps
191
+ export const createApp = async () => {
192
+ const app = express();
193
+ app.use(express.json());
202
194
 
203
- const app = express();
204
- app.use(express.json());
195
+ // Create Scenarist instance (async - returns Promise)
196
+ const scenarist = await 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 = async () => {
228
+ const { app, scenarist } = await createApp();
229
+ return { app, scenarist };
230
+ };
229
231
 
230
232
  describe("User API", () => {
231
- beforeAll(() => scenarist.start());
232
- afterAll(() => scenarist.stop());
233
+ const testContext = createTestSetup();
234
+
235
+ beforeAll(async () => {
236
+ const { scenarist } = await testContext;
237
+ scenarist?.start();
238
+ });
239
+
240
+ afterAll(async () => {
241
+ const { scenarist } = await testContext;
242
+ scenarist?.stop();
243
+ });
233
244
 
234
245
  it("should return admin user", async () => {
246
+ const { app } = await testContext;
247
+
235
248
  // Set scenario for this test
236
249
  await request(app)
237
250
  .post("/__scenario__")
@@ -255,6 +268,8 @@ describe("User API", () => {
255
268
 
256
269
  Creates a Scenarist instance with everything wired automatically.
257
270
 
271
+ **Note:** This function is **async** and returns a `Promise`. Always use `await` when calling it.
272
+
258
273
  **Parameters:**
259
274
 
260
275
  ```typescript
@@ -277,6 +292,12 @@ type ExpressAdapterOptions<T extends ScenaristScenarios> = {
277
292
 
278
293
  **Returns:**
279
294
 
295
+ ```typescript
296
+ Promise<ExpressScenarist<T> | undefined>;
297
+ ```
298
+
299
+ Where `ExpressScenarist<T>` is:
300
+
280
301
  ```typescript
281
302
  type ExpressScenarist<T extends ScenaristScenarios> = {
282
303
  config: ScenaristConfig; // Resolved configuration (endpoints, headers, etc.)
@@ -304,16 +325,20 @@ const scenarios = {
304
325
  error: errorScenario,
305
326
  } as const satisfies ScenaristScenarios;
306
327
 
307
- const scenarist = createScenarist({
328
+ // Note: createScenarist is async - use await
329
+ const scenarist = await createScenarist({
308
330
  enabled: true,
309
331
  scenarios,
310
332
  strictMode: false,
311
333
  });
312
334
 
313
- app.use(scenarist.middleware);
335
+ // Only apply middleware if scenarist is defined (not production)
336
+ if (scenarist) {
337
+ app.use(scenarist.middleware);
314
338
 
315
- beforeAll(() => scenarist.start());
316
- afterAll(() => scenarist.stop());
339
+ beforeAll(() => scenarist.start());
340
+ afterAll(() => scenarist.stop());
341
+ }
317
342
  ```
318
343
 
319
344
  ### Scenario Endpoints
@@ -626,7 +651,8 @@ const scenarios = {
626
651
  },
627
652
  } as const satisfies ScenaristScenarios;
628
653
 
629
- const scenarist = createScenarist({
654
+ // Note: createScenarist is async - use await
655
+ const scenarist = await createScenarist({
630
656
  enabled: true,
631
657
  scenarios, // 'default' key is validated at runtime
632
658
  });
@@ -648,17 +674,22 @@ export const scenarios = {
648
674
  stripeFailure: stripeFailureScenario,
649
675
  } as const satisfies ScenaristScenarios;
650
676
 
651
- // setup.ts - create scenarist with type parameter
677
+ // setup.ts - create scenarist with type parameter (async)
652
678
  import { scenarios } from "./scenarios";
653
679
 
654
- export const scenarist = createScenarist({
655
- enabled: true,
656
- scenarios, // ✅ Autocomplete + type-checked!
657
- });
680
+ // Note: createScenarist is async - must use await
681
+ export const createTestSetup = async () => {
682
+ const scenarist = await createScenarist({
683
+ enabled: true,
684
+ scenarios, // ✅ Autocomplete + type-checked!
685
+ });
686
+ return scenarist;
687
+ };
658
688
 
659
689
  // test.ts - type-safe scenario switching
660
- scenarist.switchScenario("test-123", "success"); // ✅ Autocomplete works!
661
- scenarist.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
690
+ const scenarist = await createTestSetup();
691
+ scenarist?.switchScenario("test-123", "success"); // ✅ Autocomplete works!
692
+ scenarist?.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
662
693
 
663
694
  await request(app)
664
695
  .post(scenarist.config.endpoints.setScenario)
@@ -741,7 +772,8 @@ describe("API Tests", () => {
741
772
  Enable scenario switching during development:
742
773
 
743
774
  ```typescript
744
- const scenarist = createScenarist({
775
+ // Note: createScenarist is async - use await
776
+ const scenarist = await createScenarist({
745
777
  enabled:
746
778
  process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test",
747
779
  scenarios,
@@ -766,15 +798,17 @@ curl http://localhost:3000/__scenario__
766
798
  ### Environment-Specific
767
799
 
768
800
  ```typescript
801
+ // Note: All createScenarist calls are async - use await
802
+
769
803
  // Test-only
770
- const scenarist = createScenarist({
804
+ const scenarist = await createScenarist({
771
805
  enabled: process.env.NODE_ENV === "test",
772
806
  scenarios,
773
807
  strictMode: true, // Fail if any unmocked request
774
808
  });
775
809
 
776
810
  // Development and test
777
- const scenarist = createScenarist({
811
+ const scenarist = await createScenarist({
778
812
  enabled:
779
813
  process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development",
780
814
  scenarios,
@@ -782,7 +816,7 @@ const scenarist = createScenarist({
782
816
  });
783
817
 
784
818
  // Opt-in with environment variable
785
- const scenarist = createScenarist({
819
+ const scenarist = await createScenarist({
786
820
  enabled: process.env.ENABLE_MOCKING === "true",
787
821
  scenarios,
788
822
  strictMode: false,
@@ -792,7 +826,8 @@ const scenarist = createScenarist({
792
826
  ### Custom Headers and Endpoints
793
827
 
794
828
  ```typescript
795
- const scenarist = createScenarist({
829
+ // Note: createScenarist is async - use await
830
+ const scenarist = await createScenarist({
796
831
  enabled: true,
797
832
  scenarios,
798
833
  headers: {
@@ -1033,7 +1068,8 @@ const scenarios = {
1033
1068
  myScenario: myScenario, // ✅ Registered
1034
1069
  } as const satisfies ScenaristScenarios;
1035
1070
 
1036
- const scenarist = createScenarist({
1071
+ // Note: createScenarist is async - use await
1072
+ const scenarist = await createScenarist({
1037
1073
  enabled: true,
1038
1074
  scenarios,
1039
1075
  });
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.13",
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",