@scenarist/nextjs-adapter 0.0.1 → 0.1.0
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/LICENSE +21 -0
- package/README.md +1450 -27
- package/dist/app/context.d.ts +19 -0
- package/dist/app/context.d.ts.map +1 -0
- package/dist/app/context.js +43 -0
- package/dist/app/endpoints.d.ts +20 -0
- package/dist/app/endpoints.d.ts.map +1 -0
- package/dist/app/endpoints.js +84 -0
- package/dist/app/helpers.d.ts +152 -0
- package/dist/app/helpers.d.ts.map +1 -0
- package/dist/app/helpers.js +173 -0
- package/dist/app/impl.d.ts +155 -0
- package/dist/app/impl.d.ts.map +1 -0
- package/dist/app/impl.js +97 -0
- package/dist/app/index.d.ts +40 -0
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +38 -0
- package/dist/app/production.d.ts +34 -0
- package/dist/app/production.d.ts.map +1 -0
- package/dist/app/production.js +38 -0
- package/dist/app/production.test.d.ts +100 -0
- package/dist/app/production.test.d.ts.map +1 -0
- package/dist/app/production.test.js +282 -0
- package/dist/app/setup.d.ts +29 -0
- package/dist/app/setup.d.ts.map +1 -0
- package/dist/app/setup.js +27 -0
- package/dist/common/create-scenarist-base.d.ts +32 -0
- package/dist/common/create-scenarist-base.d.ts.map +1 -0
- package/dist/common/create-scenarist-base.js +63 -0
- package/dist/common/endpoint-handlers.d.ts +99 -0
- package/dist/common/endpoint-handlers.d.ts.map +1 -0
- package/dist/common/endpoint-handlers.js +122 -0
- package/dist/pages/context.d.ts +17 -0
- package/dist/pages/context.d.ts.map +1 -0
- package/dist/pages/context.js +41 -0
- package/dist/pages/endpoints.d.ts +20 -0
- package/dist/pages/endpoints.d.ts.map +1 -0
- package/dist/pages/endpoints.js +84 -0
- package/dist/pages/helpers.d.ts +56 -0
- package/dist/pages/helpers.d.ts.map +1 -0
- package/dist/pages/helpers.js +49 -0
- package/dist/pages/impl.d.ts +124 -0
- package/dist/pages/impl.d.ts.map +1 -0
- package/dist/pages/impl.js +91 -0
- package/dist/pages/index.d.ts +37 -0
- package/dist/pages/index.d.ts.map +1 -0
- package/dist/pages/index.js +35 -0
- package/dist/pages/production.d.ts +18 -0
- package/dist/pages/production.d.ts.map +1 -0
- package/dist/pages/production.js +20 -0
- package/dist/pages/production.test.d.ts +119 -0
- package/dist/pages/production.test.d.ts.map +1 -0
- package/dist/pages/production.test.js +326 -0
- package/dist/pages/setup.d.ts +29 -0
- package/dist/pages/setup.d.ts.map +1 -0
- package/dist/pages/setup.js +27 -0
- package/package.json +86 -7
package/dist/app/impl.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { InMemoryScenarioRegistry, InMemoryScenarioStore, SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
2
|
+
import { createScenaristBase } from '../common/create-scenarist-base.js';
|
|
3
|
+
import { createScenarioEndpoint } from './endpoints.js';
|
|
4
|
+
/**
|
|
5
|
+
* Create a Scenarist instance for Next.js App Router (implementation).
|
|
6
|
+
*
|
|
7
|
+
* This is the actual implementation that wires everything automatically:
|
|
8
|
+
* - MSW server with dynamic handler
|
|
9
|
+
* - Scenario manager
|
|
10
|
+
* - State manager and sequence tracker
|
|
11
|
+
* - Response selector
|
|
12
|
+
*
|
|
13
|
+
* Unlike Express adapter, this doesn't return middleware. Instead, use `createScenarioEndpoint()`
|
|
14
|
+
* to create the handler for your app/api/%5F%5Fscenario%5F%5F/route.ts file.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // lib/scenarist.ts
|
|
19
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/app';
|
|
20
|
+
* import { defaultScenario } from './scenarios';
|
|
21
|
+
*
|
|
22
|
+
* export const scenarist = createScenarist({
|
|
23
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
24
|
+
* defaultScenario,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // app/api/%5F%5Fscenario%5F%5F/route.ts
|
|
28
|
+
* const handler = scenarist?.createScenarioEndpoint();
|
|
29
|
+
* export const POST = handler;
|
|
30
|
+
* export const GET = handler;
|
|
31
|
+
*
|
|
32
|
+
* // tests/setup.ts
|
|
33
|
+
* if (scenarist) {
|
|
34
|
+
* beforeAll(() => scenarist.start());
|
|
35
|
+
* afterAll(() => scenarist.stop());
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export const createScenaristImpl = (options) => {
|
|
40
|
+
// Singleton guard - return existing instance if already created
|
|
41
|
+
// This prevents duplicate scenario registration errors when Next.js creates multiple module instances
|
|
42
|
+
if (global.__scenarist_instance) {
|
|
43
|
+
return global.__scenarist_instance;
|
|
44
|
+
}
|
|
45
|
+
// Create or reuse global singleton stores for Next.js
|
|
46
|
+
// This ensures all module instances share the same scenario state
|
|
47
|
+
if (!global.__scenarist_registry) {
|
|
48
|
+
global.__scenarist_registry = new InMemoryScenarioRegistry();
|
|
49
|
+
}
|
|
50
|
+
if (!global.__scenarist_store) {
|
|
51
|
+
global.__scenarist_store = new InMemoryScenarioStore();
|
|
52
|
+
}
|
|
53
|
+
// Inject global singletons into base setup
|
|
54
|
+
const { config, manager, server, currentTestId } = createScenaristBase({
|
|
55
|
+
...options,
|
|
56
|
+
registry: global.__scenarist_registry,
|
|
57
|
+
store: global.__scenarist_store,
|
|
58
|
+
});
|
|
59
|
+
const instance = {
|
|
60
|
+
config,
|
|
61
|
+
switchScenario: (testId, scenarioId) => {
|
|
62
|
+
currentTestId.value = testId; // Update for MSW handler
|
|
63
|
+
return manager.switchScenario(testId, scenarioId);
|
|
64
|
+
},
|
|
65
|
+
getActiveScenario: (testId) => manager.getActiveScenario(testId),
|
|
66
|
+
getScenarioById: (scenarioId) => manager.getScenarioById(scenarioId),
|
|
67
|
+
listScenarios: () => manager.listScenarios(),
|
|
68
|
+
clearScenario: (testId) => manager.clearScenario(testId),
|
|
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
|
+
start: () => {
|
|
83
|
+
// Singleton guard - prevents duplicate MSW initialization
|
|
84
|
+
// Next.js (Turbopack) creates multiple module instances, so this ensures
|
|
85
|
+
// server.listen() is only called once across all instances
|
|
86
|
+
if (global.__scenarist_msw_started) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
global.__scenarist_msw_started = true;
|
|
90
|
+
server.listen();
|
|
91
|
+
},
|
|
92
|
+
stop: async () => server.close(),
|
|
93
|
+
};
|
|
94
|
+
// Store instance in global for singleton pattern
|
|
95
|
+
global.__scenarist_instance = instance;
|
|
96
|
+
return instance;
|
|
97
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js App Router adapter for Scenarist.
|
|
3
|
+
*
|
|
4
|
+
* Provides MSW-based scenario management for Next.js applications using App Router.
|
|
5
|
+
*
|
|
6
|
+
* @module @scenarist/nextjs-adapter/app
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // lib/scenarist.ts
|
|
11
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/app';
|
|
12
|
+
*
|
|
13
|
+
* export const scenarist = createScenarist({
|
|
14
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
15
|
+
* defaultScenario: myDefaultScenario,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // app/api/%5F%5Fscenario%5F%5F/route.ts
|
|
19
|
+
* // Note: Use %5F (URL-encoded underscore) because Next.js treats
|
|
20
|
+
* // folders starting with _ as private folders excluded from routing
|
|
21
|
+
* import { scenarist } from '@/lib/scenarist';
|
|
22
|
+
*
|
|
23
|
+
* export const POST = scenarist.createScenarioEndpoint();
|
|
24
|
+
* export const GET = scenarist.createScenarioEndpoint();
|
|
25
|
+
*
|
|
26
|
+
* // tests/setup.ts
|
|
27
|
+
* import { scenarist } from '../lib/scenarist';
|
|
28
|
+
*
|
|
29
|
+
* beforeAll(() => scenarist.start());
|
|
30
|
+
* afterAll(() => scenarist.stop());
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
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
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EACL,mBAAmB,EACnB,sCAAsC,EACtC,kBAAkB,EAClB,qCAAqC,GACtC,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAGlE,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,EACf,eAAe,GAChB,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js App Router adapter for Scenarist.
|
|
3
|
+
*
|
|
4
|
+
* Provides MSW-based scenario management for Next.js applications using App Router.
|
|
5
|
+
*
|
|
6
|
+
* @module @scenarist/nextjs-adapter/app
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // lib/scenarist.ts
|
|
11
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/app';
|
|
12
|
+
*
|
|
13
|
+
* export const scenarist = createScenarist({
|
|
14
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
15
|
+
* defaultScenario: myDefaultScenario,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // app/api/%5F%5Fscenario%5F%5F/route.ts
|
|
19
|
+
* // Note: Use %5F (URL-encoded underscore) because Next.js treats
|
|
20
|
+
* // folders starting with _ as private folders excluded from routing
|
|
21
|
+
* import { scenarist } from '@/lib/scenarist';
|
|
22
|
+
*
|
|
23
|
+
* export const POST = scenarist.createScenarioEndpoint();
|
|
24
|
+
* export const GET = scenarist.createScenarioEndpoint();
|
|
25
|
+
*
|
|
26
|
+
* // tests/setup.ts
|
|
27
|
+
* import { scenarist } from '../lib/scenarist';
|
|
28
|
+
*
|
|
29
|
+
* beforeAll(() => scenarist.start());
|
|
30
|
+
* afterAll(() => scenarist.stop());
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
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
|
+
// Re-export core types and constants for user convenience (users should only install this adapter)
|
|
38
|
+
export { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type { AppAdapterOptions, AppScenarist } from './setup.js';
|
|
2
|
+
/**
|
|
3
|
+
* Production-only entry point that returns undefined without loading test dependencies.
|
|
4
|
+
*
|
|
5
|
+
* This file provides safe stub implementations for all Scenarist functions 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.
|
|
9
|
+
*
|
|
10
|
+
* All helper functions return safe defaults (empty objects, fallback strings) so that
|
|
11
|
+
* application code can import and use them without production guards.
|
|
12
|
+
*/
|
|
13
|
+
export declare const createScenarist: (_options: import("./setup.js").AppAdapterOptions) => import("./setup.js").AppScenarist | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Production stub: Returns empty object (no test headers needed in production)
|
|
16
|
+
*/
|
|
17
|
+
export declare function getScenaristHeaders(_req: Request): Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Production stub: Returns empty object (no test headers needed in production)
|
|
20
|
+
*/
|
|
21
|
+
export declare function getScenaristHeadersFromReadonlyHeaders(_headers: {
|
|
22
|
+
get(name: string): string | null;
|
|
23
|
+
}): Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Production stub: Returns fallback test ID
|
|
26
|
+
*/
|
|
27
|
+
export declare function getScenaristTestId(_req: Request): string;
|
|
28
|
+
/**
|
|
29
|
+
* Production stub: Returns fallback test ID
|
|
30
|
+
*/
|
|
31
|
+
export declare function getScenaristTestIdFromReadonlyHeaders(_headers: {
|
|
32
|
+
get(name: string): string | null;
|
|
33
|
+
}): string;
|
|
34
|
+
//# sourceMappingURL=production.d.ts.map
|
|
@@ -0,0 +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,CACpD,QAAQ,EAAE;IAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,GAC7C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAExB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,qCAAqC,CACnD,QAAQ,EAAE;IAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,GAC7C,MAAM,CAER"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production-only entry point that returns undefined without loading test dependencies.
|
|
3
|
+
*
|
|
4
|
+
* This file provides safe stub implementations for all Scenarist functions 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.
|
|
8
|
+
*
|
|
9
|
+
* All helper functions return safe defaults (empty objects, fallback strings) so that
|
|
10
|
+
* application code can import and use them without production guards.
|
|
11
|
+
*/
|
|
12
|
+
export const createScenarist = (_options) => {
|
|
13
|
+
return undefined;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Production stub: Returns empty object (no test headers needed in production)
|
|
17
|
+
*/
|
|
18
|
+
export function getScenaristHeaders(_req) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Production stub: Returns empty object (no test headers needed in production)
|
|
23
|
+
*/
|
|
24
|
+
export function getScenaristHeadersFromReadonlyHeaders(_headers) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Production stub: Returns fallback test ID
|
|
29
|
+
*/
|
|
30
|
+
export function getScenaristTestId(_req) {
|
|
31
|
+
return 'default-test';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Production stub: Returns fallback test ID
|
|
35
|
+
*/
|
|
36
|
+
export function getScenaristTestIdFromReadonlyHeaders(_headers) {
|
|
37
|
+
return 'default-test';
|
|
38
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Entry Point Tests - Next.js App Router Adapter
|
|
3
|
+
*
|
|
4
|
+
* These tests verify the production stub implementations that enable tree-shaking
|
|
5
|
+
* of Scenarist from production builds.
|
|
6
|
+
*
|
|
7
|
+
* ## How Next.js Conditional Exports Work
|
|
8
|
+
*
|
|
9
|
+
* Next.js respects Node.js conditional exports (package.json "exports" field).
|
|
10
|
+
* When NODE_ENV=production, Next.js uses the "production" export path instead
|
|
11
|
+
* of the default export.
|
|
12
|
+
*
|
|
13
|
+
* Example from package.json:
|
|
14
|
+
* ```json
|
|
15
|
+
* {
|
|
16
|
+
* "exports": {
|
|
17
|
+
* "./app": {
|
|
18
|
+
* "production": "./dist/app/production.js",
|
|
19
|
+
* "default": "./dist/app/index.js"
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* This means:
|
|
26
|
+
* - In development/test: `import { createScenarist } from '@scenarist/nextjs-adapter/app'`
|
|
27
|
+
* resolves to `dist/app/index.js` (full Scenarist implementation)
|
|
28
|
+
* - In production: Same import resolves to `dist/app/production.js` (stubs only)
|
|
29
|
+
*
|
|
30
|
+
* ## Why This Achieves Tree-Shaking
|
|
31
|
+
*
|
|
32
|
+
* Because production.js:
|
|
33
|
+
* 1. Does NOT import from @scenarist/core or @scenarist/msw-adapter
|
|
34
|
+
* 2. Does NOT import from 'msw'
|
|
35
|
+
* 3. Only exports stub functions with zero dependencies
|
|
36
|
+
*
|
|
37
|
+
* Result: Bundlers like Next.js/Webpack can completely eliminate:
|
|
38
|
+
* - All MSW code (~50kb)
|
|
39
|
+
* - All Scenarist core logic
|
|
40
|
+
* - All scenario definitions
|
|
41
|
+
* - All test infrastructure
|
|
42
|
+
*
|
|
43
|
+
* Final production bundle: 0kb overhead from Scenarist
|
|
44
|
+
*
|
|
45
|
+
* ## Why Stubs Return Safe Defaults
|
|
46
|
+
*
|
|
47
|
+
* Application code can import and use Scenarist helpers without guards:
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Route Handler - works in ALL environments
|
|
51
|
+
* import { getScenaristHeaders } from '@scenarist/nextjs-adapter/app';
|
|
52
|
+
*
|
|
53
|
+
* export async function GET(request: Request) {
|
|
54
|
+
* const response = await fetch('http://api.example.com/data', {
|
|
55
|
+
* headers: {
|
|
56
|
+
* ...getScenaristHeaders(request), // ✅ Safe: {} in production
|
|
57
|
+
* 'x-api-key': 'abc123',
|
|
58
|
+
* },
|
|
59
|
+
* });
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* In production, `getScenaristHeaders()` returns `{}`, so spreading it is a no-op.
|
|
64
|
+
* No runtime errors, no guards needed, same code everywhere.
|
|
65
|
+
*
|
|
66
|
+
* ## Related Documentation
|
|
67
|
+
*
|
|
68
|
+
* **Core Feature (Node.js Conditional Exports):**
|
|
69
|
+
* - https://nodejs.org/api/packages.html#conditional-exports
|
|
70
|
+
* Defines how package.json "exports" field works with conditions like "production"
|
|
71
|
+
* - https://nodejs.org/api/packages.html#exports
|
|
72
|
+
* Complete specification for package.json exports field
|
|
73
|
+
*
|
|
74
|
+
* **Next.js Build Optimizations:**
|
|
75
|
+
* - https://nextjs.org/blog/next-14-2
|
|
76
|
+
* Next.js 14.2 tree-shaking improvements (see "Improved Tree Shaking" section)
|
|
77
|
+
* - https://nextjs.org/docs/app/guides/package-bundling
|
|
78
|
+
* Next.js package bundling and optimization strategies
|
|
79
|
+
*
|
|
80
|
+
* **Bundler Documentation:**
|
|
81
|
+
* - https://webpack.js.org/configuration/mode/#mode-production
|
|
82
|
+
* Webpack production mode (sets NODE_ENV=production, enables conditional exports)
|
|
83
|
+
* - https://turbo.build/pack/docs
|
|
84
|
+
* Turbopack (Next.js's new bundler, also respects conditional exports)
|
|
85
|
+
*
|
|
86
|
+
* **Note:** Next.js doesn't document conditional exports specifically because it's a
|
|
87
|
+
* Node.js feature that Next.js (via Webpack/Turbopack) automatically respects during
|
|
88
|
+
* production builds. The key is that NODE_ENV=production triggers the "production"
|
|
89
|
+
* export path in package.json.
|
|
90
|
+
*
|
|
91
|
+
* ## Testing Strategy
|
|
92
|
+
*
|
|
93
|
+
* These tests verify:
|
|
94
|
+
* 1. All stub functions return safe, expected defaults
|
|
95
|
+
* 2. createScenarist returns undefined (no Scenarist instance in production)
|
|
96
|
+
* 3. Helper functions can be called without errors
|
|
97
|
+
* 4. No imports from test dependencies (tree-shaking verification)
|
|
98
|
+
*/
|
|
99
|
+
export {};
|
|
100
|
+
//# sourceMappingURL=production.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"production.test.d.ts","sourceRoot":"","sources":["../../src/app/production.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiGG"}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Entry Point Tests - Next.js App Router Adapter
|
|
3
|
+
*
|
|
4
|
+
* These tests verify the production stub implementations that enable tree-shaking
|
|
5
|
+
* of Scenarist from production builds.
|
|
6
|
+
*
|
|
7
|
+
* ## How Next.js Conditional Exports Work
|
|
8
|
+
*
|
|
9
|
+
* Next.js respects Node.js conditional exports (package.json "exports" field).
|
|
10
|
+
* When NODE_ENV=production, Next.js uses the "production" export path instead
|
|
11
|
+
* of the default export.
|
|
12
|
+
*
|
|
13
|
+
* Example from package.json:
|
|
14
|
+
* ```json
|
|
15
|
+
* {
|
|
16
|
+
* "exports": {
|
|
17
|
+
* "./app": {
|
|
18
|
+
* "production": "./dist/app/production.js",
|
|
19
|
+
* "default": "./dist/app/index.js"
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* This means:
|
|
26
|
+
* - In development/test: `import { createScenarist } from '@scenarist/nextjs-adapter/app'`
|
|
27
|
+
* resolves to `dist/app/index.js` (full Scenarist implementation)
|
|
28
|
+
* - In production: Same import resolves to `dist/app/production.js` (stubs only)
|
|
29
|
+
*
|
|
30
|
+
* ## Why This Achieves Tree-Shaking
|
|
31
|
+
*
|
|
32
|
+
* Because production.js:
|
|
33
|
+
* 1. Does NOT import from @scenarist/core or @scenarist/msw-adapter
|
|
34
|
+
* 2. Does NOT import from 'msw'
|
|
35
|
+
* 3. Only exports stub functions with zero dependencies
|
|
36
|
+
*
|
|
37
|
+
* Result: Bundlers like Next.js/Webpack can completely eliminate:
|
|
38
|
+
* - All MSW code (~50kb)
|
|
39
|
+
* - All Scenarist core logic
|
|
40
|
+
* - All scenario definitions
|
|
41
|
+
* - All test infrastructure
|
|
42
|
+
*
|
|
43
|
+
* Final production bundle: 0kb overhead from Scenarist
|
|
44
|
+
*
|
|
45
|
+
* ## Why Stubs Return Safe Defaults
|
|
46
|
+
*
|
|
47
|
+
* Application code can import and use Scenarist helpers without guards:
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Route Handler - works in ALL environments
|
|
51
|
+
* import { getScenaristHeaders } from '@scenarist/nextjs-adapter/app';
|
|
52
|
+
*
|
|
53
|
+
* export async function GET(request: Request) {
|
|
54
|
+
* const response = await fetch('http://api.example.com/data', {
|
|
55
|
+
* headers: {
|
|
56
|
+
* ...getScenaristHeaders(request), // ✅ Safe: {} in production
|
|
57
|
+
* 'x-api-key': 'abc123',
|
|
58
|
+
* },
|
|
59
|
+
* });
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* In production, `getScenaristHeaders()` returns `{}`, so spreading it is a no-op.
|
|
64
|
+
* No runtime errors, no guards needed, same code everywhere.
|
|
65
|
+
*
|
|
66
|
+
* ## Related Documentation
|
|
67
|
+
*
|
|
68
|
+
* **Core Feature (Node.js Conditional Exports):**
|
|
69
|
+
* - https://nodejs.org/api/packages.html#conditional-exports
|
|
70
|
+
* Defines how package.json "exports" field works with conditions like "production"
|
|
71
|
+
* - https://nodejs.org/api/packages.html#exports
|
|
72
|
+
* Complete specification for package.json exports field
|
|
73
|
+
*
|
|
74
|
+
* **Next.js Build Optimizations:**
|
|
75
|
+
* - https://nextjs.org/blog/next-14-2
|
|
76
|
+
* Next.js 14.2 tree-shaking improvements (see "Improved Tree Shaking" section)
|
|
77
|
+
* - https://nextjs.org/docs/app/guides/package-bundling
|
|
78
|
+
* Next.js package bundling and optimization strategies
|
|
79
|
+
*
|
|
80
|
+
* **Bundler Documentation:**
|
|
81
|
+
* - https://webpack.js.org/configuration/mode/#mode-production
|
|
82
|
+
* Webpack production mode (sets NODE_ENV=production, enables conditional exports)
|
|
83
|
+
* - https://turbo.build/pack/docs
|
|
84
|
+
* Turbopack (Next.js's new bundler, also respects conditional exports)
|
|
85
|
+
*
|
|
86
|
+
* **Note:** Next.js doesn't document conditional exports specifically because it's a
|
|
87
|
+
* Node.js feature that Next.js (via Webpack/Turbopack) automatically respects during
|
|
88
|
+
* production builds. The key is that NODE_ENV=production triggers the "production"
|
|
89
|
+
* export path in package.json.
|
|
90
|
+
*
|
|
91
|
+
* ## Testing Strategy
|
|
92
|
+
*
|
|
93
|
+
* These tests verify:
|
|
94
|
+
* 1. All stub functions return safe, expected defaults
|
|
95
|
+
* 2. createScenarist returns undefined (no Scenarist instance in production)
|
|
96
|
+
* 3. Helper functions can be called without errors
|
|
97
|
+
* 4. No imports from test dependencies (tree-shaking verification)
|
|
98
|
+
*/
|
|
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
|
+
// In production builds, Scenarist should not exist at all
|
|
105
|
+
// Routes check: if (scenarist) { scenarist.createScenarioEndpoint() }
|
|
106
|
+
const scenarist = production.createScenarist({
|
|
107
|
+
enabled: true,
|
|
108
|
+
scenarios: {
|
|
109
|
+
test: {
|
|
110
|
+
id: 'test',
|
|
111
|
+
name: 'Test Scenario',
|
|
112
|
+
description: 'A test scenario',
|
|
113
|
+
mocks: [],
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
expect(scenarist).toBeUndefined();
|
|
118
|
+
});
|
|
119
|
+
it('should accept same options as development version (type safety)', () => {
|
|
120
|
+
// This ensures the production stub has the same TypeScript signature
|
|
121
|
+
// as the real implementation, preventing type errors in user code
|
|
122
|
+
const scenarist = production.createScenarist({
|
|
123
|
+
enabled: false,
|
|
124
|
+
scenarios: {
|
|
125
|
+
default: {
|
|
126
|
+
id: 'default',
|
|
127
|
+
name: 'Default Scenario',
|
|
128
|
+
description: 'Default test scenario',
|
|
129
|
+
mocks: [],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
defaultTestId: 'test-123',
|
|
133
|
+
});
|
|
134
|
+
expect(scenarist).toBeUndefined();
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe('getScenaristHeaders', () => {
|
|
138
|
+
it('should return empty object (no test headers in production)', () => {
|
|
139
|
+
const mockRequest = new Request('http://localhost:3000', {
|
|
140
|
+
headers: {
|
|
141
|
+
'x-scenarist-test-id': 'test-123',
|
|
142
|
+
'x-user-id': 'user-456',
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
const headers = production.getScenaristHeaders(mockRequest);
|
|
146
|
+
// Production: No test infrastructure headers added
|
|
147
|
+
expect(headers).toEqual({});
|
|
148
|
+
expect(Object.keys(headers)).toHaveLength(0);
|
|
149
|
+
});
|
|
150
|
+
it('should be safe to spread in fetch headers', () => {
|
|
151
|
+
const mockRequest = new Request('http://localhost:3000');
|
|
152
|
+
const headers = production.getScenaristHeaders(mockRequest);
|
|
153
|
+
// This pattern works in all environments without guards
|
|
154
|
+
const fetchHeaders = {
|
|
155
|
+
'content-type': 'application/json',
|
|
156
|
+
...headers, // Spreading {} is a no-op
|
|
157
|
+
};
|
|
158
|
+
expect(fetchHeaders).toEqual({
|
|
159
|
+
'content-type': 'application/json',
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
describe('getScenaristHeadersFromReadonlyHeaders', () => {
|
|
164
|
+
it('should return empty object for ReadonlyHeaders (Server Components)', () => {
|
|
165
|
+
// Simulate ReadonlyHeaders from next/headers
|
|
166
|
+
const mockReadonlyHeaders = {
|
|
167
|
+
get: (name) => (name === 'x-scenarist-test-id' ? 'test-123' : null),
|
|
168
|
+
};
|
|
169
|
+
const headers = production.getScenaristHeadersFromReadonlyHeaders(mockReadonlyHeaders);
|
|
170
|
+
expect(headers).toEqual({});
|
|
171
|
+
expect(Object.keys(headers)).toHaveLength(0);
|
|
172
|
+
});
|
|
173
|
+
it('should be safe to spread in Server Component fetch headers', () => {
|
|
174
|
+
const mockReadonlyHeaders = {
|
|
175
|
+
get: (_name) => null,
|
|
176
|
+
};
|
|
177
|
+
const headers = production.getScenaristHeadersFromReadonlyHeaders(mockReadonlyHeaders);
|
|
178
|
+
const fetchHeaders = {
|
|
179
|
+
'x-api-key': 'secret',
|
|
180
|
+
...headers, // Spreading {} is a no-op
|
|
181
|
+
};
|
|
182
|
+
expect(fetchHeaders).toEqual({
|
|
183
|
+
'x-api-key': 'secret',
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
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
|
+
});
|
|
192
|
+
const testId = production.getScenaristTestId(mockRequest);
|
|
193
|
+
// In production, always returns fallback (no test isolation needed)
|
|
194
|
+
expect(testId).toBe('default-test');
|
|
195
|
+
});
|
|
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
|
+
});
|
|
200
|
+
const request2 = new Request('http://localhost:3000', {
|
|
201
|
+
headers: { 'x-scenarist-test-id': 'test-xyz' },
|
|
202
|
+
});
|
|
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
|
+
});
|
|
208
|
+
});
|
|
209
|
+
describe('getScenaristTestIdFromReadonlyHeaders', () => {
|
|
210
|
+
it('should return fallback test ID for ReadonlyHeaders (Server Components)', () => {
|
|
211
|
+
const mockReadonlyHeaders = {
|
|
212
|
+
get: (name) => (name === 'x-scenarist-test-id' ? 'test-123' : null),
|
|
213
|
+
};
|
|
214
|
+
const testId = production.getScenaristTestIdFromReadonlyHeaders(mockReadonlyHeaders);
|
|
215
|
+
expect(testId).toBe('default-test');
|
|
216
|
+
});
|
|
217
|
+
it('should be usable for logging/debugging in production', () => {
|
|
218
|
+
const mockReadonlyHeaders = {
|
|
219
|
+
get: (_name) => null,
|
|
220
|
+
};
|
|
221
|
+
const testId = production.getScenaristTestIdFromReadonlyHeaders(mockReadonlyHeaders);
|
|
222
|
+
// Even in production, code can safely call this for logging
|
|
223
|
+
const logMessage = `Processing request for test: ${testId}`;
|
|
224
|
+
expect(logMessage).toBe('Processing request for test: default-test');
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
describe('tree-shaking verification', () => {
|
|
228
|
+
it('should export only stub functions with no test dependencies', () => {
|
|
229
|
+
// Verify all expected exports exist
|
|
230
|
+
expect(production.createScenarist).toBeDefined();
|
|
231
|
+
expect(production.getScenaristHeaders).toBeDefined();
|
|
232
|
+
expect(production.getScenaristHeadersFromReadonlyHeaders).toBeDefined();
|
|
233
|
+
expect(production.getScenaristTestId).toBeDefined();
|
|
234
|
+
expect(production.getScenaristTestIdFromReadonlyHeaders).toBeDefined();
|
|
235
|
+
// Verify stubs are simple functions (not complex objects)
|
|
236
|
+
expect(typeof production.createScenarist).toBe('function');
|
|
237
|
+
expect(typeof production.getScenaristHeaders).toBe('function');
|
|
238
|
+
expect(typeof production.getScenaristTestId).toBe('function');
|
|
239
|
+
});
|
|
240
|
+
it('should not have any Scenarist instance methods', () => {
|
|
241
|
+
const scenarist = production.createScenarist({
|
|
242
|
+
enabled: true,
|
|
243
|
+
scenarios: {
|
|
244
|
+
default: {
|
|
245
|
+
id: 'default',
|
|
246
|
+
name: 'Default',
|
|
247
|
+
description: 'Default scenario',
|
|
248
|
+
mocks: [],
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
// Production stub returns undefined, not a Scenarist instance
|
|
253
|
+
expect(scenarist).toBeUndefined();
|
|
254
|
+
// These methods exist in development but NOT in production
|
|
255
|
+
// Optional chaining prevents errors when accessing properties on undefined
|
|
256
|
+
expect(scenarist?.start).toBeUndefined();
|
|
257
|
+
expect(scenarist?.stop).toBeUndefined();
|
|
258
|
+
expect(scenarist?.createScenarioEndpoint).toBeUndefined();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
describe('production safety', () => {
|
|
262
|
+
it('should handle being called multiple times without errors', () => {
|
|
263
|
+
// Production code might call these helpers frequently
|
|
264
|
+
const mockRequest = new Request('http://localhost:3000');
|
|
265
|
+
expect(() => {
|
|
266
|
+
for (let i = 0; i < 100; i++) {
|
|
267
|
+
production.getScenaristHeaders(mockRequest);
|
|
268
|
+
production.getScenaristTestId(mockRequest);
|
|
269
|
+
}
|
|
270
|
+
}).not.toThrow();
|
|
271
|
+
});
|
|
272
|
+
it('should return consistent results for same inputs', () => {
|
|
273
|
+
const mockRequest = new Request('http://localhost:3000');
|
|
274
|
+
const headers1 = production.getScenaristHeaders(mockRequest);
|
|
275
|
+
const headers2 = production.getScenaristHeaders(mockRequest);
|
|
276
|
+
const testId1 = production.getScenaristTestId(mockRequest);
|
|
277
|
+
const testId2 = production.getScenaristTestId(mockRequest);
|
|
278
|
+
expect(headers1).toEqual(headers2);
|
|
279
|
+
expect(testId1).toBe(testId2);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type { AppAdapterOptions, AppScenarist } from './impl.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create a Scenarist instance for Next.js App Router.
|
|
4
|
+
*
|
|
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)
|
|
8
|
+
*
|
|
9
|
+
* This ensures all test code is eliminated from production bundles.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // lib/scenarist.ts
|
|
14
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/app';
|
|
15
|
+
* import { scenarios } from './scenarios';
|
|
16
|
+
*
|
|
17
|
+
* export const scenarist = createScenarist({
|
|
18
|
+
* enabled: true,
|
|
19
|
+
* scenarios,
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Start MSW in Node.js environment
|
|
23
|
+
* if (typeof window === 'undefined') {
|
|
24
|
+
* scenarist.start();
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export { createScenaristImpl as createScenarist } from './impl.js';
|
|
29
|
+
//# sourceMappingURL=setup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/app/setup.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,mBAAmB,IAAI,eAAe,EAAE,MAAM,WAAW,CAAC"}
|