@scenarist/nextjs-adapter 0.1.7 → 0.1.9
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 +335 -234
- package/dist/app/context.d.ts +1 -1
- package/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +2 -2
- package/dist/app/endpoints.d.ts +1 -1
- package/dist/app/endpoints.d.ts.map +1 -1
- package/dist/app/endpoints.js +5 -5
- package/dist/app/helpers.d.ts +0 -10
- package/dist/app/helpers.d.ts.map +1 -1
- package/dist/app/helpers.js +18 -14
- package/dist/app/impl.d.ts +3 -68
- package/dist/app/impl.d.ts.map +1 -1
- package/dist/app/impl.js +3 -15
- package/dist/app/index.d.ts +7 -7
- package/dist/app/index.js +5 -5
- package/dist/app/production.d.ts +1 -1
- package/dist/app/production.d.ts.map +1 -1
- package/dist/app/production.js +2 -2
- package/dist/app/production.test.js +62 -62
- package/dist/app/setup.d.ts +2 -2
- package/dist/app/setup.js +1 -1
- package/dist/common/create-scenarist-base.d.ts +2 -2
- package/dist/common/create-scenarist-base.d.ts.map +1 -1
- package/dist/common/create-scenarist-base.js +7 -6
- package/dist/common/endpoint-handlers.d.ts +1 -1
- package/dist/common/endpoint-handlers.js +5 -5
- package/dist/pages/context.d.ts +2 -2
- package/dist/pages/context.d.ts.map +1 -1
- package/dist/pages/context.js +5 -5
- package/dist/pages/endpoints.d.ts +2 -2
- package/dist/pages/endpoints.d.ts.map +1 -1
- package/dist/pages/endpoints.js +5 -5
- package/dist/pages/helpers.d.ts +2 -7
- package/dist/pages/helpers.d.ts.map +1 -1
- package/dist/pages/helpers.js +16 -6
- package/dist/pages/impl.d.ts +3 -41
- package/dist/pages/impl.d.ts.map +1 -1
- package/dist/pages/impl.js +3 -10
- package/dist/pages/index.d.ts +7 -7
- package/dist/pages/index.js +5 -5
- package/dist/pages/production.d.ts +2 -2
- package/dist/pages/production.test.js +78 -70
- package/dist/pages/setup.d.ts +2 -2
- package/dist/pages/setup.js +1 -1
- package/package.json +3 -3
package/dist/app/context.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;GAQG;AACH,qBAAa,iBAAkB,YAAW,cAAc;IAEpD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,eAAe;IAG1C,SAAS,IAAI,MAAM;IAUnB,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAU3D,WAAW,IAAI,MAAM;CAUtB"}
|
package/dist/app/context.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SCENARIST_TEST_ID_HEADER } from
|
|
1
|
+
import { SCENARIST_TEST_ID_HEADER, } from "@scenarist/core";
|
|
2
2
|
/**
|
|
3
3
|
* RequestContext implementation for Next.js App Router.
|
|
4
4
|
*
|
|
@@ -37,7 +37,7 @@ export class AppRequestContext {
|
|
|
37
37
|
catch {
|
|
38
38
|
// Return empty string for malformed URLs (e.g., relative URLs without base)
|
|
39
39
|
// This is expected behavior - hostname extraction is best-effort
|
|
40
|
-
return
|
|
40
|
+
return "";
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
package/dist/app/endpoints.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoints.d.ts","sourceRoot":"","sources":["../../src/app/endpoints.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"endpoints.d.ts","sourceRoot":"","sources":["../../src/app/endpoints.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAwExE;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,sBAAsB,GACjC,SAAS,eAAe,EACxB,QAAQ,eAAe,MAKT,KAAK,OAAO,KAAG,OAAO,CAAC,QAAQ,CAgB9C,CAAC"}
|
package/dist/app/endpoints.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AppRequestContext } from
|
|
2
|
-
import { handlePostLogic, handleGetLogic } from
|
|
1
|
+
import { AppRequestContext } from "./context.js";
|
|
2
|
+
import { handlePostLogic, handleGetLogic, } from "../common/endpoint-handlers.js";
|
|
3
3
|
/**
|
|
4
4
|
* Handle POST request to switch scenarios.
|
|
5
5
|
*
|
|
@@ -71,14 +71,14 @@ export const createScenarioEndpoint = (manager, config) => {
|
|
|
71
71
|
const postHandler = handlePost(manager, config);
|
|
72
72
|
const getHandler = handleGet(manager, config);
|
|
73
73
|
return async (req) => {
|
|
74
|
-
if (req.method ===
|
|
74
|
+
if (req.method === "POST") {
|
|
75
75
|
return postHandler(req);
|
|
76
76
|
}
|
|
77
|
-
if (req.method ===
|
|
77
|
+
if (req.method === "GET") {
|
|
78
78
|
return getHandler(req);
|
|
79
79
|
}
|
|
80
80
|
return Response.json({
|
|
81
|
-
error:
|
|
81
|
+
error: "Method not allowed",
|
|
82
82
|
}, { status: 405 });
|
|
83
83
|
};
|
|
84
84
|
};
|
package/dist/app/helpers.d.ts
CHANGED
|
@@ -10,11 +10,6 @@
|
|
|
10
10
|
* - Safe to spread in fetch headers without guards
|
|
11
11
|
* - Zero runtime overhead (tree-shaken in production builds)
|
|
12
12
|
*
|
|
13
|
-
* **Why use this instead of scenarist.getHeaders()?**
|
|
14
|
-
* - No need to check if scenarist is defined (`scenarist?.getHeaders(req) ?? {}`)
|
|
15
|
-
* - Works automatically by accessing the global singleton
|
|
16
|
-
* - Consistent API across development, test, and production
|
|
17
|
-
*
|
|
18
13
|
* @param req - The Web standard Request object
|
|
19
14
|
* @returns Object with single entry: configured test ID header name → value from request or default
|
|
20
15
|
* Returns `{}` in production when scenarist is undefined
|
|
@@ -47,11 +42,6 @@ export declare function getScenaristHeaders(req: Request): Record<string, string
|
|
|
47
42
|
* - Safe to spread in fetch headers without guards
|
|
48
43
|
* - Zero runtime overhead (tree-shaken in production builds)
|
|
49
44
|
*
|
|
50
|
-
* **Why use this instead of scenarist.getHeadersFromReadonlyHeaders()?**
|
|
51
|
-
* - No need to check if scenarist is defined
|
|
52
|
-
* - Works automatically by accessing the global singleton
|
|
53
|
-
* - Consistent API across development, test, and production
|
|
54
|
-
*
|
|
55
45
|
* @param headers - The ReadonlyHeaders object from headers() in 'next/headers'
|
|
56
46
|
* @returns Object with single entry: configured test ID header name → value from headers or default
|
|
57
47
|
* Returns `{}` in production when scenarist is undefined
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/app/helpers.ts"],"names":[],"mappings":"AAQA
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/app/helpers.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAaxE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,sCAAsC,CAAC,OAAO,EAAE;IAC9D,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAClC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAazB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,qCAAqC,CAAC,OAAO,EAAE;IAC7D,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAClC,GAAG,MAAM,CAST;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CASvD"}
|
package/dist/app/helpers.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { SCENARIST_TEST_ID_HEADER } from
|
|
1
|
+
import { SCENARIST_TEST_ID_HEADER } from "@scenarist/core";
|
|
2
2
|
/**
|
|
3
3
|
* Fallback constant for default test ID when scenarist is undefined (production builds).
|
|
4
4
|
* In development/test, this is overridden by values from scenarist.config.
|
|
5
5
|
*/
|
|
6
|
-
const FALLBACK_DEFAULT_TEST_ID =
|
|
6
|
+
const FALLBACK_DEFAULT_TEST_ID = "default-test";
|
|
7
7
|
/**
|
|
8
8
|
* Safe helper to extract Scenarist infrastructure headers from a Request object.
|
|
9
9
|
*
|
|
@@ -16,11 +16,6 @@ const FALLBACK_DEFAULT_TEST_ID = 'default-test';
|
|
|
16
16
|
* - Safe to spread in fetch headers without guards
|
|
17
17
|
* - Zero runtime overhead (tree-shaken in production builds)
|
|
18
18
|
*
|
|
19
|
-
* **Why use this instead of scenarist.getHeaders()?**
|
|
20
|
-
* - No need to check if scenarist is defined (`scenarist?.getHeaders(req) ?? {}`)
|
|
21
|
-
* - Works automatically by accessing the global singleton
|
|
22
|
-
* - Consistent API across development, test, and production
|
|
23
|
-
*
|
|
24
19
|
* @param req - The Web standard Request object
|
|
25
20
|
* @returns Object with single entry: configured test ID header name → value from request or default
|
|
26
21
|
* Returns `{}` in production when scenarist is undefined
|
|
@@ -42,7 +37,14 @@ const FALLBACK_DEFAULT_TEST_ID = 'default-test';
|
|
|
42
37
|
*/
|
|
43
38
|
export function getScenaristHeaders(req) {
|
|
44
39
|
const scenarist = global.__scenarist_instance;
|
|
45
|
-
|
|
40
|
+
if (!scenarist) {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
const defaultTestId = scenarist.config?.defaultTestId ?? FALLBACK_DEFAULT_TEST_ID;
|
|
44
|
+
const testId = req.headers.get(SCENARIST_TEST_ID_HEADER) ?? defaultTestId;
|
|
45
|
+
return {
|
|
46
|
+
[SCENARIST_TEST_ID_HEADER]: testId,
|
|
47
|
+
};
|
|
46
48
|
}
|
|
47
49
|
/**
|
|
48
50
|
* Safe helper to extract Scenarist infrastructure headers from ReadonlyHeaders.
|
|
@@ -56,11 +58,6 @@ export function getScenaristHeaders(req) {
|
|
|
56
58
|
* - Safe to spread in fetch headers without guards
|
|
57
59
|
* - Zero runtime overhead (tree-shaken in production builds)
|
|
58
60
|
*
|
|
59
|
-
* **Why use this instead of scenarist.getHeadersFromReadonlyHeaders()?**
|
|
60
|
-
* - No need to check if scenarist is defined
|
|
61
|
-
* - Works automatically by accessing the global singleton
|
|
62
|
-
* - Consistent API across development, test, and production
|
|
63
|
-
*
|
|
64
61
|
* @param headers - The ReadonlyHeaders object from headers() in 'next/headers'
|
|
65
62
|
* @returns Object with single entry: configured test ID header name → value from headers or default
|
|
66
63
|
* Returns `{}` in production when scenarist is undefined
|
|
@@ -88,7 +85,14 @@ export function getScenaristHeaders(req) {
|
|
|
88
85
|
*/
|
|
89
86
|
export function getScenaristHeadersFromReadonlyHeaders(headers) {
|
|
90
87
|
const scenarist = global.__scenarist_instance;
|
|
91
|
-
|
|
88
|
+
if (!scenarist) {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
const defaultTestId = scenarist.config?.defaultTestId ?? FALLBACK_DEFAULT_TEST_ID;
|
|
92
|
+
const testId = headers.get(SCENARIST_TEST_ID_HEADER) ?? defaultTestId;
|
|
93
|
+
return {
|
|
94
|
+
[SCENARIST_TEST_ID_HEADER]: testId,
|
|
95
|
+
};
|
|
92
96
|
}
|
|
93
97
|
/**
|
|
94
98
|
* Safe helper to extract just the test ID from ReadonlyHeaders.
|
package/dist/app/impl.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { BaseAdapterOptions, ScenaristAdapter, ScenarioRegistry, ScenarioStore } from
|
|
2
|
-
import { createScenarioEndpoint } from
|
|
1
|
+
import type { BaseAdapterOptions, ScenaristAdapter, ScenarioRegistry, ScenarioStore } from "@scenarist/core";
|
|
2
|
+
import { createScenarioEndpoint } from "./endpoints.js";
|
|
3
3
|
/**
|
|
4
4
|
* Global state for Next.js App Router adapter.
|
|
5
5
|
*
|
|
@@ -35,7 +35,7 @@ export type AppAdapterOptions = BaseAdapterOptions;
|
|
|
35
35
|
* Provides MSW server lifecycle management and scenario endpoint factory.
|
|
36
36
|
* Unlike Express adapter, doesn't provide middleware (Next.js doesn't have global middleware for App Router).
|
|
37
37
|
*/
|
|
38
|
-
export type AppScenarist = Omit<ScenaristAdapter<never>,
|
|
38
|
+
export type AppScenarist = Omit<ScenaristAdapter<never>, "middleware"> & {
|
|
39
39
|
/**
|
|
40
40
|
* Create scenario endpoint handler for use in app/api/%5F%5Fscenario%5F%5F/route.ts
|
|
41
41
|
*
|
|
@@ -50,71 +50,6 @@ export type AppScenarist = Omit<ScenaristAdapter<never>, 'middleware'> & {
|
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
52
|
createScenarioEndpoint: () => ReturnType<typeof createScenarioEndpoint>;
|
|
53
|
-
/**
|
|
54
|
-
* Extract Scenarist infrastructure headers from the request.
|
|
55
|
-
*
|
|
56
|
-
* This helper respects the configured test ID header name and default test ID,
|
|
57
|
-
* ensuring headers are forwarded correctly when making external API calls.
|
|
58
|
-
*
|
|
59
|
-
* Works with Web standard Request objects (App Router, Server Components, Route Handlers).
|
|
60
|
-
*
|
|
61
|
-
* @param req - The Web standard Request object
|
|
62
|
-
* @returns Object with single entry: configured test ID header name → value from request or default
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* // Route Handler
|
|
67
|
-
* export async function GET(request: Request) {
|
|
68
|
-
* const response = await fetch('http://localhost:3001/products', {
|
|
69
|
-
* headers: scenarist.getHeaders(request),
|
|
70
|
-
* });
|
|
71
|
-
* }
|
|
72
|
-
*
|
|
73
|
-
* // Server Component
|
|
74
|
-
* async function ProductsPage() {
|
|
75
|
-
* const response = await fetch('http://localhost:3001/products', {
|
|
76
|
-
* headers: scenarist.getHeaders(request),
|
|
77
|
-
* });
|
|
78
|
-
* }
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
getHeaders: (req: Request) => Record<string, string>;
|
|
82
|
-
/**
|
|
83
|
-
* Extract Scenarist infrastructure headers from ReadonlyHeaders.
|
|
84
|
-
*
|
|
85
|
-
* This helper is designed for Next.js Server Components that use headers() from 'next/headers',
|
|
86
|
-
* which returns ReadonlyHeaders (not a Request object).
|
|
87
|
-
*
|
|
88
|
-
* Respects the configured test ID header name and default test ID,
|
|
89
|
-
* ensuring headers are forwarded correctly when making external API calls.
|
|
90
|
-
*
|
|
91
|
-
* @param headers - The ReadonlyHeaders object from headers() in 'next/headers'
|
|
92
|
-
* @returns Object with single entry: configured test ID header name → value from headers or default
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* // Server Component
|
|
97
|
-
* import { headers } from 'next/headers';
|
|
98
|
-
* import { scenarist } from '@/lib/scenarist';
|
|
99
|
-
*
|
|
100
|
-
* export default async function ProductsPage() {
|
|
101
|
-
* const headersList = await headers();
|
|
102
|
-
*
|
|
103
|
-
* const response = await fetch('http://localhost:3001/products', {
|
|
104
|
-
* headers: {
|
|
105
|
-
* ...scenarist.getHeadersFromReadonlyHeaders(headersList),
|
|
106
|
-
* 'x-user-tier': 'premium',
|
|
107
|
-
* },
|
|
108
|
-
* });
|
|
109
|
-
*
|
|
110
|
-
* const data = await response.json();
|
|
111
|
-
* return <ProductList products={data.products} />;
|
|
112
|
-
* }
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
getHeadersFromReadonlyHeaders: (headers: {
|
|
116
|
-
get(name: string): string | null;
|
|
117
|
-
}) => Record<string, string>;
|
|
118
53
|
};
|
|
119
54
|
/**
|
|
120
55
|
* Create a Scenarist instance for Next.js App Router (implementation).
|
package/dist/app/impl.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"impl.d.ts","sourceRoot":"","sources":["../../src/app/impl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"impl.d.ts","sourceRoot":"","sources":["../../src/app/impl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACd,MAAM,iBAAiB,CAAC;AAMzB,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,CAAC,MAAM,CAAC;IAEb,IAAI,uBAAuB,EAAE,OAAO,GAAG,SAAS,CAAC;IAEjD,IAAI,oBAAoB,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAEvD,IAAI,iBAAiB,EAAE,aAAa,GAAG,SAAS,CAAC;IAEjD,IAAI,oBAAoB,EAAE,YAAY,GAAG,SAAS,CAAC;CACpD;AAED;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,kBAAkB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,GAAG;IACvE;;;;;;;;;;;;OAYG;IACH,sBAAsB,EAAE,MAAM,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;CACzE,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,iBAAiB,KACzB,YAoDF,CAAC"}
|
package/dist/app/impl.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { InMemoryScenarioRegistry, InMemoryScenarioStore,
|
|
2
|
-
import { createScenaristBase } from
|
|
3
|
-
import { createScenarioEndpoint } from
|
|
1
|
+
import { InMemoryScenarioRegistry, InMemoryScenarioStore, } from "@scenarist/core";
|
|
2
|
+
import { createScenaristBase } from "../common/create-scenarist-base.js";
|
|
3
|
+
import { createScenarioEndpoint } from "./endpoints.js";
|
|
4
4
|
/**
|
|
5
5
|
* Create a Scenarist instance for Next.js App Router (implementation).
|
|
6
6
|
*
|
|
@@ -67,18 +67,6 @@ export const createScenaristImpl = (options) => {
|
|
|
67
67
|
listScenarios: () => manager.listScenarios(),
|
|
68
68
|
clearScenario: (testId) => manager.clearScenario(testId),
|
|
69
69
|
createScenarioEndpoint: () => createScenarioEndpoint(manager, config),
|
|
70
|
-
getHeaders: (req) => {
|
|
71
|
-
const testId = req.headers.get(SCENARIST_TEST_ID_HEADER) || config.defaultTestId;
|
|
72
|
-
return {
|
|
73
|
-
[SCENARIST_TEST_ID_HEADER]: testId,
|
|
74
|
-
};
|
|
75
|
-
},
|
|
76
|
-
getHeadersFromReadonlyHeaders: (headers) => {
|
|
77
|
-
const testId = headers.get(SCENARIST_TEST_ID_HEADER) || config.defaultTestId;
|
|
78
|
-
return {
|
|
79
|
-
[SCENARIST_TEST_ID_HEADER]: testId,
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
70
|
start: () => {
|
|
83
71
|
// Singleton guard - prevents duplicate MSW initialization
|
|
84
72
|
// Next.js (Turbopack) creates multiple module instances, so this ensures
|
package/dist/app/index.d.ts
CHANGED
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
* afterAll(() => scenarist.stop());
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
|
-
export { createScenarist } from
|
|
34
|
-
export { AppRequestContext } from
|
|
35
|
-
export { createScenarioEndpoint } from
|
|
36
|
-
export { getScenaristHeaders, getScenaristHeadersFromReadonlyHeaders, getScenaristTestId, getScenaristTestIdFromReadonlyHeaders, } from
|
|
37
|
-
export type { AppAdapterOptions, AppScenarist } from
|
|
38
|
-
export { SCENARIST_TEST_ID_HEADER } from
|
|
39
|
-
export type { ScenaristScenario, ScenaristMock, ScenaristResponse, ScenaristSequence, ScenaristMatch, ScenaristCaptureConfig, ScenaristScenarios, ScenaristConfig, ScenaristResult, } from
|
|
33
|
+
export { createScenarist } from "./setup.js";
|
|
34
|
+
export { AppRequestContext } from "./context.js";
|
|
35
|
+
export { createScenarioEndpoint } from "./endpoints.js";
|
|
36
|
+
export { getScenaristHeaders, getScenaristHeadersFromReadonlyHeaders, getScenaristTestId, getScenaristTestIdFromReadonlyHeaders, } from "./helpers.js";
|
|
37
|
+
export type { AppAdapterOptions, AppScenarist } from "./setup.js";
|
|
38
|
+
export { SCENARIST_TEST_ID_HEADER } from "@scenarist/core";
|
|
39
|
+
export type { ScenaristScenario, ScenaristMock, ScenaristResponse, ScenaristSequence, ScenaristMatch, ScenaristCaptureConfig, ScenaristScenarios, ScenaristConfig, ScenaristResult, } from "@scenarist/core";
|
|
40
40
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/app/index.js
CHANGED
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
* afterAll(() => scenarist.stop());
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
|
-
export { createScenarist } from
|
|
34
|
-
export { AppRequestContext } from
|
|
35
|
-
export { createScenarioEndpoint } from
|
|
36
|
-
export { getScenaristHeaders, getScenaristHeadersFromReadonlyHeaders, getScenaristTestId, getScenaristTestIdFromReadonlyHeaders, } from
|
|
33
|
+
export { createScenarist } from "./setup.js";
|
|
34
|
+
export { AppRequestContext } from "./context.js";
|
|
35
|
+
export { createScenarioEndpoint } from "./endpoints.js";
|
|
36
|
+
export { getScenaristHeaders, getScenaristHeadersFromReadonlyHeaders, getScenaristTestId, getScenaristTestIdFromReadonlyHeaders, } from "./helpers.js";
|
|
37
37
|
// Re-export core types and constants for user convenience (users should only install this adapter)
|
|
38
|
-
export { SCENARIST_TEST_ID_HEADER } from
|
|
38
|
+
export { SCENARIST_TEST_ID_HEADER } from "@scenarist/core";
|
package/dist/app/production.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/app/production.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,OAAO,YAAY,EAAE,iBAAiB,KAC/C,OAAO,YAAY,EAAE,YAAY,GAAG,SAEtC,CAAC;AAEF;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEzE;AAED;;GAEG;AACH,wBAAgB,sCAAsC,
|
|
1
|
+
{"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/app/production.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,OAAO,YAAY,EAAE,iBAAiB,KAC/C,OAAO,YAAY,EAAE,YAAY,GAAG,SAEtC,CAAC;AAEF;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEzE;AAED;;GAEG;AACH,wBAAgB,sCAAsC,CAAC,QAAQ,EAAE;IAC/D,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAClC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEzB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,qCAAqC,CAAC,QAAQ,EAAE;IAC9D,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAClC,GAAG,MAAM,CAET"}
|
package/dist/app/production.js
CHANGED
|
@@ -28,11 +28,11 @@ export function getScenaristHeadersFromReadonlyHeaders(_headers) {
|
|
|
28
28
|
* Production stub: Returns fallback test ID
|
|
29
29
|
*/
|
|
30
30
|
export function getScenaristTestId(_req) {
|
|
31
|
-
return
|
|
31
|
+
return "default-test";
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Production stub: Returns fallback test ID
|
|
35
35
|
*/
|
|
36
36
|
export function getScenaristTestIdFromReadonlyHeaders(_headers) {
|
|
37
|
-
return
|
|
37
|
+
return "default-test";
|
|
38
38
|
}
|
|
@@ -96,50 +96,50 @@
|
|
|
96
96
|
* 3. Helper functions can be called without errors
|
|
97
97
|
* 4. No imports from test dependencies (tree-shaking verification)
|
|
98
98
|
*/
|
|
99
|
-
import { describe, it, expect } from
|
|
100
|
-
import * as production from
|
|
101
|
-
describe(
|
|
102
|
-
describe(
|
|
103
|
-
it(
|
|
99
|
+
import { describe, it, expect } from "vitest";
|
|
100
|
+
import * as production from "./production.js";
|
|
101
|
+
describe("App Router production entry point", () => {
|
|
102
|
+
describe("createScenarist", () => {
|
|
103
|
+
it("should return undefined (Scenarist disabled in production)", () => {
|
|
104
104
|
// In production builds, Scenarist should not exist at all
|
|
105
105
|
// Routes check: if (scenarist) { scenarist.createScenarioEndpoint() }
|
|
106
106
|
const scenarist = production.createScenarist({
|
|
107
107
|
enabled: true,
|
|
108
108
|
scenarios: {
|
|
109
109
|
test: {
|
|
110
|
-
id:
|
|
111
|
-
name:
|
|
112
|
-
description:
|
|
110
|
+
id: "test",
|
|
111
|
+
name: "Test Scenario",
|
|
112
|
+
description: "A test scenario",
|
|
113
113
|
mocks: [],
|
|
114
114
|
},
|
|
115
115
|
},
|
|
116
116
|
});
|
|
117
117
|
expect(scenarist).toBeUndefined();
|
|
118
118
|
});
|
|
119
|
-
it(
|
|
119
|
+
it("should accept same options as development version (type safety)", () => {
|
|
120
120
|
// This ensures the production stub has the same TypeScript signature
|
|
121
121
|
// as the real implementation, preventing type errors in user code
|
|
122
122
|
const scenarist = production.createScenarist({
|
|
123
123
|
enabled: false,
|
|
124
124
|
scenarios: {
|
|
125
125
|
default: {
|
|
126
|
-
id:
|
|
127
|
-
name:
|
|
128
|
-
description:
|
|
126
|
+
id: "default",
|
|
127
|
+
name: "Default Scenario",
|
|
128
|
+
description: "Default test scenario",
|
|
129
129
|
mocks: [],
|
|
130
130
|
},
|
|
131
131
|
},
|
|
132
|
-
defaultTestId:
|
|
132
|
+
defaultTestId: "test-123",
|
|
133
133
|
});
|
|
134
134
|
expect(scenarist).toBeUndefined();
|
|
135
135
|
});
|
|
136
136
|
});
|
|
137
|
-
describe(
|
|
138
|
-
it(
|
|
139
|
-
const mockRequest = new Request(
|
|
137
|
+
describe("getScenaristHeaders", () => {
|
|
138
|
+
it("should return empty object (no test headers in production)", () => {
|
|
139
|
+
const mockRequest = new Request("http://localhost:3000", {
|
|
140
140
|
headers: {
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
"x-scenarist-test-id": "test-123",
|
|
142
|
+
"x-user-id": "user-456",
|
|
143
143
|
},
|
|
144
144
|
});
|
|
145
145
|
const headers = production.getScenaristHeaders(mockRequest);
|
|
@@ -147,85 +147,85 @@ describe('App Router production entry point', () => {
|
|
|
147
147
|
expect(headers).toEqual({});
|
|
148
148
|
expect(Object.keys(headers)).toHaveLength(0);
|
|
149
149
|
});
|
|
150
|
-
it(
|
|
151
|
-
const mockRequest = new Request(
|
|
150
|
+
it("should be safe to spread in fetch headers", () => {
|
|
151
|
+
const mockRequest = new Request("http://localhost:3000");
|
|
152
152
|
const headers = production.getScenaristHeaders(mockRequest);
|
|
153
153
|
// This pattern works in all environments without guards
|
|
154
154
|
const fetchHeaders = {
|
|
155
|
-
|
|
155
|
+
"content-type": "application/json",
|
|
156
156
|
...headers, // Spreading {} is a no-op
|
|
157
157
|
};
|
|
158
158
|
expect(fetchHeaders).toEqual({
|
|
159
|
-
|
|
159
|
+
"content-type": "application/json",
|
|
160
160
|
});
|
|
161
161
|
});
|
|
162
162
|
});
|
|
163
|
-
describe(
|
|
164
|
-
it(
|
|
163
|
+
describe("getScenaristHeadersFromReadonlyHeaders", () => {
|
|
164
|
+
it("should return empty object for ReadonlyHeaders (Server Components)", () => {
|
|
165
165
|
// Simulate ReadonlyHeaders from next/headers
|
|
166
166
|
const mockReadonlyHeaders = {
|
|
167
|
-
get: (name) =>
|
|
167
|
+
get: (name) => name === "x-scenarist-test-id" ? "test-123" : null,
|
|
168
168
|
};
|
|
169
169
|
const headers = production.getScenaristHeadersFromReadonlyHeaders(mockReadonlyHeaders);
|
|
170
170
|
expect(headers).toEqual({});
|
|
171
171
|
expect(Object.keys(headers)).toHaveLength(0);
|
|
172
172
|
});
|
|
173
|
-
it(
|
|
173
|
+
it("should be safe to spread in Server Component fetch headers", () => {
|
|
174
174
|
const mockReadonlyHeaders = {
|
|
175
175
|
get: (_name) => null,
|
|
176
176
|
};
|
|
177
177
|
const headers = production.getScenaristHeadersFromReadonlyHeaders(mockReadonlyHeaders);
|
|
178
178
|
const fetchHeaders = {
|
|
179
|
-
|
|
179
|
+
"x-api-key": "secret",
|
|
180
180
|
...headers, // Spreading {} is a no-op
|
|
181
181
|
};
|
|
182
182
|
expect(fetchHeaders).toEqual({
|
|
183
|
-
|
|
183
|
+
"x-api-key": "secret",
|
|
184
184
|
});
|
|
185
185
|
});
|
|
186
186
|
});
|
|
187
|
-
describe(
|
|
188
|
-
it(
|
|
189
|
-
const mockRequest = new Request(
|
|
190
|
-
headers: {
|
|
187
|
+
describe("getScenaristTestId", () => {
|
|
188
|
+
it("should return fallback test ID for Request", () => {
|
|
189
|
+
const mockRequest = new Request("http://localhost:3000", {
|
|
190
|
+
headers: { "x-scenarist-test-id": "test-123" },
|
|
191
191
|
});
|
|
192
192
|
const testId = production.getScenaristTestId(mockRequest);
|
|
193
193
|
// In production, always returns fallback (no test isolation needed)
|
|
194
|
-
expect(testId).toBe(
|
|
194
|
+
expect(testId).toBe("default-test");
|
|
195
195
|
});
|
|
196
|
-
it(
|
|
197
|
-
const request1 = new Request(
|
|
198
|
-
headers: {
|
|
196
|
+
it("should always return same fallback regardless of headers", () => {
|
|
197
|
+
const request1 = new Request("http://localhost:3000", {
|
|
198
|
+
headers: { "x-scenarist-test-id": "test-abc" },
|
|
199
199
|
});
|
|
200
|
-
const request2 = new Request(
|
|
201
|
-
headers: {
|
|
200
|
+
const request2 = new Request("http://localhost:3000", {
|
|
201
|
+
headers: { "x-scenarist-test-id": "test-xyz" },
|
|
202
202
|
});
|
|
203
|
-
const request3 = new Request(
|
|
204
|
-
expect(production.getScenaristTestId(request1)).toBe(
|
|
205
|
-
expect(production.getScenaristTestId(request2)).toBe(
|
|
206
|
-
expect(production.getScenaristTestId(request3)).toBe(
|
|
203
|
+
const request3 = new Request("http://localhost:3000"); // No headers
|
|
204
|
+
expect(production.getScenaristTestId(request1)).toBe("default-test");
|
|
205
|
+
expect(production.getScenaristTestId(request2)).toBe("default-test");
|
|
206
|
+
expect(production.getScenaristTestId(request3)).toBe("default-test");
|
|
207
207
|
});
|
|
208
208
|
});
|
|
209
|
-
describe(
|
|
210
|
-
it(
|
|
209
|
+
describe("getScenaristTestIdFromReadonlyHeaders", () => {
|
|
210
|
+
it("should return fallback test ID for ReadonlyHeaders (Server Components)", () => {
|
|
211
211
|
const mockReadonlyHeaders = {
|
|
212
|
-
get: (name) =>
|
|
212
|
+
get: (name) => name === "x-scenarist-test-id" ? "test-123" : null,
|
|
213
213
|
};
|
|
214
214
|
const testId = production.getScenaristTestIdFromReadonlyHeaders(mockReadonlyHeaders);
|
|
215
|
-
expect(testId).toBe(
|
|
215
|
+
expect(testId).toBe("default-test");
|
|
216
216
|
});
|
|
217
|
-
it(
|
|
217
|
+
it("should be usable for logging/debugging in production", () => {
|
|
218
218
|
const mockReadonlyHeaders = {
|
|
219
219
|
get: (_name) => null,
|
|
220
220
|
};
|
|
221
221
|
const testId = production.getScenaristTestIdFromReadonlyHeaders(mockReadonlyHeaders);
|
|
222
222
|
// Even in production, code can safely call this for logging
|
|
223
223
|
const logMessage = `Processing request for test: ${testId}`;
|
|
224
|
-
expect(logMessage).toBe(
|
|
224
|
+
expect(logMessage).toBe("Processing request for test: default-test");
|
|
225
225
|
});
|
|
226
226
|
});
|
|
227
|
-
describe(
|
|
228
|
-
it(
|
|
227
|
+
describe("tree-shaking verification", () => {
|
|
228
|
+
it("should export only stub functions with no test dependencies", () => {
|
|
229
229
|
// Verify all expected exports exist
|
|
230
230
|
expect(production.createScenarist).toBeDefined();
|
|
231
231
|
expect(production.getScenaristHeaders).toBeDefined();
|
|
@@ -233,18 +233,18 @@ describe('App Router production entry point', () => {
|
|
|
233
233
|
expect(production.getScenaristTestId).toBeDefined();
|
|
234
234
|
expect(production.getScenaristTestIdFromReadonlyHeaders).toBeDefined();
|
|
235
235
|
// Verify stubs are simple functions (not complex objects)
|
|
236
|
-
expect(typeof production.createScenarist).toBe(
|
|
237
|
-
expect(typeof production.getScenaristHeaders).toBe(
|
|
238
|
-
expect(typeof production.getScenaristTestId).toBe(
|
|
236
|
+
expect(typeof production.createScenarist).toBe("function");
|
|
237
|
+
expect(typeof production.getScenaristHeaders).toBe("function");
|
|
238
|
+
expect(typeof production.getScenaristTestId).toBe("function");
|
|
239
239
|
});
|
|
240
|
-
it(
|
|
240
|
+
it("should not have any Scenarist instance methods", () => {
|
|
241
241
|
const scenarist = production.createScenarist({
|
|
242
242
|
enabled: true,
|
|
243
243
|
scenarios: {
|
|
244
244
|
default: {
|
|
245
|
-
id:
|
|
246
|
-
name:
|
|
247
|
-
description:
|
|
245
|
+
id: "default",
|
|
246
|
+
name: "Default",
|
|
247
|
+
description: "Default scenario",
|
|
248
248
|
mocks: [],
|
|
249
249
|
},
|
|
250
250
|
},
|
|
@@ -258,10 +258,10 @@ describe('App Router production entry point', () => {
|
|
|
258
258
|
expect(scenarist?.createScenarioEndpoint).toBeUndefined();
|
|
259
259
|
});
|
|
260
260
|
});
|
|
261
|
-
describe(
|
|
262
|
-
it(
|
|
261
|
+
describe("production safety", () => {
|
|
262
|
+
it("should handle being called multiple times without errors", () => {
|
|
263
263
|
// Production code might call these helpers frequently
|
|
264
|
-
const mockRequest = new Request(
|
|
264
|
+
const mockRequest = new Request("http://localhost:3000");
|
|
265
265
|
expect(() => {
|
|
266
266
|
for (let i = 0; i < 100; i++) {
|
|
267
267
|
production.getScenaristHeaders(mockRequest);
|
|
@@ -269,8 +269,8 @@ describe('App Router production entry point', () => {
|
|
|
269
269
|
}
|
|
270
270
|
}).not.toThrow();
|
|
271
271
|
});
|
|
272
|
-
it(
|
|
273
|
-
const mockRequest = new Request(
|
|
272
|
+
it("should return consistent results for same inputs", () => {
|
|
273
|
+
const mockRequest = new Request("http://localhost:3000");
|
|
274
274
|
const headers1 = production.getScenaristHeaders(mockRequest);
|
|
275
275
|
const headers2 = production.getScenaristHeaders(mockRequest);
|
|
276
276
|
const testId1 = production.getScenaristTestId(mockRequest);
|
package/dist/app/setup.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { AppAdapterOptions, AppScenarist } from
|
|
1
|
+
export type { AppAdapterOptions, AppScenarist } from "./impl.js";
|
|
2
2
|
/**
|
|
3
3
|
* Create a Scenarist instance for Next.js App Router.
|
|
4
4
|
*
|
|
@@ -25,5 +25,5 @@ export type { AppAdapterOptions, AppScenarist } from './impl.js';
|
|
|
25
25
|
* }
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export { createScenaristImpl as createScenarist } from
|
|
28
|
+
export { createScenaristImpl as createScenarist } from "./impl.js";
|
|
29
29
|
//# sourceMappingURL=setup.d.ts.map
|
package/dist/app/setup.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { setupServer } from
|
|
2
|
-
import { type BaseAdapterOptions, type ScenaristConfig, type ScenarioManager, type ResponseSelector } from
|
|
1
|
+
import { setupServer } from "msw/node";
|
|
2
|
+
import { type BaseAdapterOptions, type ScenaristConfig, type ScenarioManager, type ResponseSelector } from "@scenarist/core";
|
|
3
3
|
/**
|
|
4
4
|
* Shared setup logic for both Pages Router and App Router adapters.
|
|
5
5
|
*
|