@scenarist/express-adapter 0.1.13 → 0.1.15
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 +22 -34
- package/dist/setup/production.d.ts +7 -4
- package/dist/setup/production.d.ts.map +1 -1
- package/dist/setup/production.js +6 -2
- package/dist/setup/setup-scenarist.d.ts +14 -11
- package/dist/setup/setup-scenarist.d.ts.map +1 -1
- package/dist/setup/setup-scenarist.js +14 -19
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -187,13 +187,13 @@ import express from "express";
|
|
|
187
187
|
import { createScenarist } from "@scenarist/express-adapter";
|
|
188
188
|
import { scenarios } from "./scenarios";
|
|
189
189
|
|
|
190
|
-
//
|
|
191
|
-
export const createApp =
|
|
190
|
+
// Factory function for Express app setup
|
|
191
|
+
export const createApp = () => {
|
|
192
192
|
const app = express();
|
|
193
193
|
app.use(express.json());
|
|
194
194
|
|
|
195
|
-
// Create Scenarist instance (
|
|
196
|
-
const scenarist =
|
|
195
|
+
// Create Scenarist instance (synchronous)
|
|
196
|
+
const scenarist = createScenarist({
|
|
197
197
|
enabled: process.env.NODE_ENV === "test",
|
|
198
198
|
scenarios, // All scenarios registered upfront
|
|
199
199
|
strictMode: false,
|
|
@@ -224,27 +224,23 @@ import request from "supertest";
|
|
|
224
224
|
import { createApp } from "../src/app";
|
|
225
225
|
|
|
226
226
|
// Factory function for test setup - no let variables
|
|
227
|
-
const createTestSetup =
|
|
228
|
-
const { app, scenarist } =
|
|
227
|
+
const createTestSetup = () => {
|
|
228
|
+
const { app, scenarist } = createApp();
|
|
229
229
|
return { app, scenarist };
|
|
230
230
|
};
|
|
231
231
|
|
|
232
232
|
describe("User API", () => {
|
|
233
|
-
const
|
|
233
|
+
const { app, scenarist } = createTestSetup();
|
|
234
234
|
|
|
235
|
-
beforeAll(
|
|
236
|
-
const { scenarist } = await testContext;
|
|
235
|
+
beforeAll(() => {
|
|
237
236
|
scenarist?.start();
|
|
238
237
|
});
|
|
239
238
|
|
|
240
239
|
afterAll(async () => {
|
|
241
|
-
|
|
242
|
-
scenarist?.stop();
|
|
240
|
+
await scenarist?.stop();
|
|
243
241
|
});
|
|
244
242
|
|
|
245
243
|
it("should return admin user", async () => {
|
|
246
|
-
const { app } = await testContext;
|
|
247
|
-
|
|
248
244
|
// Set scenario for this test
|
|
249
245
|
await request(app)
|
|
250
246
|
.post("/__scenario__")
|
|
@@ -268,7 +264,7 @@ describe("User API", () => {
|
|
|
268
264
|
|
|
269
265
|
Creates a Scenarist instance with everything wired automatically.
|
|
270
266
|
|
|
271
|
-
**Note:** This function is **
|
|
267
|
+
**Note:** This function is **synchronous** and returns the instance directly (or `undefined` in production).
|
|
272
268
|
|
|
273
269
|
**Parameters:**
|
|
274
270
|
|
|
@@ -325,8 +321,7 @@ const scenarios = {
|
|
|
325
321
|
error: errorScenario,
|
|
326
322
|
} as const satisfies ScenaristScenarios;
|
|
327
323
|
|
|
328
|
-
|
|
329
|
-
const scenarist = await createScenarist({
|
|
324
|
+
const scenarist = createScenarist({
|
|
330
325
|
enabled: true,
|
|
331
326
|
scenarios,
|
|
332
327
|
strictMode: false,
|
|
@@ -651,8 +646,7 @@ const scenarios = {
|
|
|
651
646
|
},
|
|
652
647
|
} as const satisfies ScenaristScenarios;
|
|
653
648
|
|
|
654
|
-
|
|
655
|
-
const scenarist = await createScenarist({
|
|
649
|
+
const scenarist = createScenarist({
|
|
656
650
|
enabled: true,
|
|
657
651
|
scenarios, // 'default' key is validated at runtime
|
|
658
652
|
});
|
|
@@ -674,12 +668,11 @@ export const scenarios = {
|
|
|
674
668
|
stripeFailure: stripeFailureScenario,
|
|
675
669
|
} as const satisfies ScenaristScenarios;
|
|
676
670
|
|
|
677
|
-
// setup.ts - create scenarist with type parameter
|
|
671
|
+
// setup.ts - create scenarist with type parameter
|
|
678
672
|
import { scenarios } from "./scenarios";
|
|
679
673
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
const scenarist = await createScenarist({
|
|
674
|
+
export const createTestSetup = () => {
|
|
675
|
+
const scenarist = createScenarist({
|
|
683
676
|
enabled: true,
|
|
684
677
|
scenarios, // ✅ Autocomplete + type-checked!
|
|
685
678
|
});
|
|
@@ -687,7 +680,7 @@ export const createTestSetup = async () => {
|
|
|
687
680
|
};
|
|
688
681
|
|
|
689
682
|
// test.ts - type-safe scenario switching
|
|
690
|
-
const scenarist =
|
|
683
|
+
const scenarist = createTestSetup();
|
|
691
684
|
scenarist?.switchScenario("test-123", "success"); // ✅ Autocomplete works!
|
|
692
685
|
scenarist?.switchScenario("test-123", "invalid-name"); // ❌ TypeScript error!
|
|
693
686
|
|
|
@@ -772,8 +765,7 @@ describe("API Tests", () => {
|
|
|
772
765
|
Enable scenario switching during development:
|
|
773
766
|
|
|
774
767
|
```typescript
|
|
775
|
-
|
|
776
|
-
const scenarist = await createScenarist({
|
|
768
|
+
const scenarist = createScenarist({
|
|
777
769
|
enabled:
|
|
778
770
|
process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test",
|
|
779
771
|
scenarios,
|
|
@@ -798,17 +790,15 @@ curl http://localhost:3000/__scenario__
|
|
|
798
790
|
### Environment-Specific
|
|
799
791
|
|
|
800
792
|
```typescript
|
|
801
|
-
// Note: All createScenarist calls are async - use await
|
|
802
|
-
|
|
803
793
|
// Test-only
|
|
804
|
-
const scenarist =
|
|
794
|
+
const scenarist = createScenarist({
|
|
805
795
|
enabled: process.env.NODE_ENV === "test",
|
|
806
796
|
scenarios,
|
|
807
797
|
strictMode: true, // Fail if any unmocked request
|
|
808
798
|
});
|
|
809
799
|
|
|
810
800
|
// Development and test
|
|
811
|
-
const scenarist =
|
|
801
|
+
const scenarist = createScenarist({
|
|
812
802
|
enabled:
|
|
813
803
|
process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development",
|
|
814
804
|
scenarios,
|
|
@@ -816,7 +806,7 @@ const scenarist = await createScenarist({
|
|
|
816
806
|
});
|
|
817
807
|
|
|
818
808
|
// Opt-in with environment variable
|
|
819
|
-
const scenarist =
|
|
809
|
+
const scenarist = createScenarist({
|
|
820
810
|
enabled: process.env.ENABLE_MOCKING === "true",
|
|
821
811
|
scenarios,
|
|
822
812
|
strictMode: false,
|
|
@@ -826,8 +816,7 @@ const scenarist = await createScenarist({
|
|
|
826
816
|
### Custom Headers and Endpoints
|
|
827
817
|
|
|
828
818
|
```typescript
|
|
829
|
-
|
|
830
|
-
const scenarist = await createScenarist({
|
|
819
|
+
const scenarist = createScenarist({
|
|
831
820
|
enabled: true,
|
|
832
821
|
scenarios,
|
|
833
822
|
headers: {
|
|
@@ -1068,8 +1057,7 @@ const scenarios = {
|
|
|
1068
1057
|
myScenario: myScenario, // ✅ Registered
|
|
1069
1058
|
} as const satisfies ScenaristScenarios;
|
|
1070
1059
|
|
|
1071
|
-
|
|
1072
|
-
const scenarist = await createScenarist({
|
|
1060
|
+
const scenarist = createScenarist({
|
|
1073
1061
|
enabled: true,
|
|
1074
1062
|
scenarios,
|
|
1075
1063
|
});
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
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
|
-
*
|
|
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:
|
|
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":"
|
|
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"}
|
package/dist/setup/production.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Production-only entry point that returns undefined without loading any dependencies.
|
|
3
|
-
*
|
|
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 =
|
|
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
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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":"
|
|
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
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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.
|
|
3
|
+
"version": "0.1.15",
|
|
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/core": "0.1.
|
|
52
|
-
"@scenarist/msw-adapter": "0.1.
|
|
51
|
+
"@scenarist/core": "0.1.3",
|
|
52
|
+
"@scenarist/msw-adapter": "0.1.3"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"express": "^4.18.0 || ^5.0.0",
|