@scenarist/express-adapter 0.1.11 → 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.
- package/README.md +97 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,17 @@
|
|
|
5
5
|
|
|
6
6
|
Express.js adapter for [Scenarist](https://scenarist.io) - manage MSW mock scenarios in your Express applications for testing **and** development.
|
|
7
7
|
|
|
8
|
+
## 📖 Documentation
|
|
9
|
+
|
|
10
|
+
**→ [Express Getting Started Guide](https://scenarist.io/frameworks/express/getting-started)** — Full setup instructions and examples
|
|
11
|
+
|
|
12
|
+
| Topic | Link |
|
|
13
|
+
| --------------------- | ------------------------------------------------------------------------------------------------ |
|
|
14
|
+
| **Why Scenarist?** | [scenarist.io/getting-started/why-scenarist](https://scenarist.io/getting-started/why-scenarist) |
|
|
15
|
+
| **Tool Comparison** | [scenarist.io/comparison](https://scenarist.io/comparison) |
|
|
16
|
+
| **Parallel Testing** | [scenarist.io/testing/parallel-testing](https://scenarist.io/testing/parallel-testing) |
|
|
17
|
+
| **Dynamic Responses** | [scenarist.io/concepts/dynamic-responses](https://scenarist.io/concepts/dynamic-responses) |
|
|
18
|
+
|
|
8
19
|
## What is Scenarist?
|
|
9
20
|
|
|
10
21
|
**Scenarist** enables concurrent tests to run with different backend states by switching mock scenarios at runtime via test IDs. Your real application code executes while external API responses are controlled by scenarios. No application restarts needed, no complex per-test mocking, just simple scenario switching.
|
|
@@ -171,56 +182,69 @@ export const scenarios = {
|
|
|
171
182
|
### 2. Create Scenarist Instance
|
|
172
183
|
|
|
173
184
|
```typescript
|
|
174
|
-
//
|
|
185
|
+
// src/app.ts
|
|
186
|
+
import express from "express";
|
|
175
187
|
import { createScenarist } from "@scenarist/express-adapter";
|
|
176
188
|
import { scenarios } from "./scenarios";
|
|
177
189
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
### 3. Add to Express App
|
|
190
|
+
// Use async factory pattern for Express apps
|
|
191
|
+
export const createApp = async () => {
|
|
192
|
+
const app = express();
|
|
193
|
+
app.use(express.json());
|
|
186
194
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
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
|
+
});
|
|
194
201
|
|
|
195
|
-
// Add Scenarist middleware (
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
}
|
|
202
|
+
// Add Scenarist middleware (only if enabled)
|
|
203
|
+
if (scenarist) {
|
|
204
|
+
app.use(scenarist.middleware);
|
|
205
|
+
}
|
|
199
206
|
|
|
200
|
-
// Your application routes
|
|
201
|
-
app.get("/api/user", async (req, res) => {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
});
|
|
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
|
+
});
|
|
206
213
|
|
|
207
|
-
|
|
214
|
+
return { app, scenarist };
|
|
215
|
+
};
|
|
208
216
|
```
|
|
209
217
|
|
|
210
|
-
###
|
|
218
|
+
### 3. Use in Tests
|
|
211
219
|
|
|
212
220
|
```typescript
|
|
213
221
|
// test/api.test.ts
|
|
214
222
|
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
|
215
223
|
import request from "supertest";
|
|
216
|
-
import {
|
|
217
|
-
|
|
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
|
+
};
|
|
218
231
|
|
|
219
232
|
describe("User API", () => {
|
|
220
|
-
|
|
221
|
-
|
|
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
|
+
});
|
|
222
244
|
|
|
223
245
|
it("should return admin user", async () => {
|
|
246
|
+
const { app } = await testContext;
|
|
247
|
+
|
|
224
248
|
// Set scenario for this test
|
|
225
249
|
await request(app)
|
|
226
250
|
.post("/__scenario__")
|
|
@@ -244,6 +268,8 @@ describe("User API", () => {
|
|
|
244
268
|
|
|
245
269
|
Creates a Scenarist instance with everything wired automatically.
|
|
246
270
|
|
|
271
|
+
**Note:** This function is **async** and returns a `Promise`. Always use `await` when calling it.
|
|
272
|
+
|
|
247
273
|
**Parameters:**
|
|
248
274
|
|
|
249
275
|
```typescript
|
|
@@ -266,6 +292,12 @@ type ExpressAdapterOptions<T extends ScenaristScenarios> = {
|
|
|
266
292
|
|
|
267
293
|
**Returns:**
|
|
268
294
|
|
|
295
|
+
```typescript
|
|
296
|
+
Promise<ExpressScenarist<T> | undefined>;
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Where `ExpressScenarist<T>` is:
|
|
300
|
+
|
|
269
301
|
```typescript
|
|
270
302
|
type ExpressScenarist<T extends ScenaristScenarios> = {
|
|
271
303
|
config: ScenaristConfig; // Resolved configuration (endpoints, headers, etc.)
|
|
@@ -293,16 +325,20 @@ const scenarios = {
|
|
|
293
325
|
error: errorScenario,
|
|
294
326
|
} as const satisfies ScenaristScenarios;
|
|
295
327
|
|
|
296
|
-
|
|
328
|
+
// Note: createScenarist is async - use await
|
|
329
|
+
const scenarist = await createScenarist({
|
|
297
330
|
enabled: true,
|
|
298
331
|
scenarios,
|
|
299
332
|
strictMode: false,
|
|
300
333
|
});
|
|
301
334
|
|
|
302
|
-
|
|
335
|
+
// Only apply middleware if scenarist is defined (not production)
|
|
336
|
+
if (scenarist) {
|
|
337
|
+
app.use(scenarist.middleware);
|
|
303
338
|
|
|
304
|
-
beforeAll(() => scenarist.start());
|
|
305
|
-
afterAll(() => scenarist.stop());
|
|
339
|
+
beforeAll(() => scenarist.start());
|
|
340
|
+
afterAll(() => scenarist.stop());
|
|
341
|
+
}
|
|
306
342
|
```
|
|
307
343
|
|
|
308
344
|
### Scenario Endpoints
|
|
@@ -615,7 +651,8 @@ const scenarios = {
|
|
|
615
651
|
},
|
|
616
652
|
} as const satisfies ScenaristScenarios;
|
|
617
653
|
|
|
618
|
-
|
|
654
|
+
// Note: createScenarist is async - use await
|
|
655
|
+
const scenarist = await createScenarist({
|
|
619
656
|
enabled: true,
|
|
620
657
|
scenarios, // 'default' key is validated at runtime
|
|
621
658
|
});
|
|
@@ -637,17 +674,22 @@ export const scenarios = {
|
|
|
637
674
|
stripeFailure: stripeFailureScenario,
|
|
638
675
|
} as const satisfies ScenaristScenarios;
|
|
639
676
|
|
|
640
|
-
// setup.ts - create scenarist with type parameter
|
|
677
|
+
// setup.ts - create scenarist with type parameter (async)
|
|
641
678
|
import { scenarios } from "./scenarios";
|
|
642
679
|
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
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
|
+
};
|
|
647
688
|
|
|
648
689
|
// test.ts - type-safe scenario switching
|
|
649
|
-
scenarist
|
|
650
|
-
scenarist
|
|
690
|
+
const scenarist = await createTestSetup();
|
|
691
|
+
scenarist?.switchScenario("test-123", "success"); // ✅ Autocomplete works!
|
|
692
|
+
scenarist?.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
|
|
651
693
|
|
|
652
694
|
await request(app)
|
|
653
695
|
.post(scenarist.config.endpoints.setScenario)
|
|
@@ -730,7 +772,8 @@ describe("API Tests", () => {
|
|
|
730
772
|
Enable scenario switching during development:
|
|
731
773
|
|
|
732
774
|
```typescript
|
|
733
|
-
|
|
775
|
+
// Note: createScenarist is async - use await
|
|
776
|
+
const scenarist = await createScenarist({
|
|
734
777
|
enabled:
|
|
735
778
|
process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test",
|
|
736
779
|
scenarios,
|
|
@@ -755,15 +798,17 @@ curl http://localhost:3000/__scenario__
|
|
|
755
798
|
### Environment-Specific
|
|
756
799
|
|
|
757
800
|
```typescript
|
|
801
|
+
// Note: All createScenarist calls are async - use await
|
|
802
|
+
|
|
758
803
|
// Test-only
|
|
759
|
-
const scenarist = createScenarist({
|
|
804
|
+
const scenarist = await createScenarist({
|
|
760
805
|
enabled: process.env.NODE_ENV === "test",
|
|
761
806
|
scenarios,
|
|
762
807
|
strictMode: true, // Fail if any unmocked request
|
|
763
808
|
});
|
|
764
809
|
|
|
765
810
|
// Development and test
|
|
766
|
-
const scenarist = createScenarist({
|
|
811
|
+
const scenarist = await createScenarist({
|
|
767
812
|
enabled:
|
|
768
813
|
process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development",
|
|
769
814
|
scenarios,
|
|
@@ -771,7 +816,7 @@ const scenarist = createScenarist({
|
|
|
771
816
|
});
|
|
772
817
|
|
|
773
818
|
// Opt-in with environment variable
|
|
774
|
-
const scenarist = createScenarist({
|
|
819
|
+
const scenarist = await createScenarist({
|
|
775
820
|
enabled: process.env.ENABLE_MOCKING === "true",
|
|
776
821
|
scenarios,
|
|
777
822
|
strictMode: false,
|
|
@@ -781,7 +826,8 @@ const scenarist = createScenarist({
|
|
|
781
826
|
### Custom Headers and Endpoints
|
|
782
827
|
|
|
783
828
|
```typescript
|
|
784
|
-
|
|
829
|
+
// Note: createScenarist is async - use await
|
|
830
|
+
const scenarist = await createScenarist({
|
|
785
831
|
enabled: true,
|
|
786
832
|
scenarios,
|
|
787
833
|
headers: {
|
|
@@ -1022,7 +1068,8 @@ const scenarios = {
|
|
|
1022
1068
|
myScenario: myScenario, // ✅ Registered
|
|
1023
1069
|
} as const satisfies ScenaristScenarios;
|
|
1024
1070
|
|
|
1025
|
-
|
|
1071
|
+
// Note: createScenarist is async - use await
|
|
1072
|
+
const scenarist = await createScenarist({
|
|
1026
1073
|
enabled: true,
|
|
1027
1074
|
scenarios,
|
|
1028
1075
|
});
|