@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
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'http';
|
|
2
|
+
import type { BaseAdapterOptions, ScenaristAdapter, ScenarioRegistry, ScenarioStore } from '@scenarist/core';
|
|
3
|
+
import { createScenarioEndpoint } from './endpoints.js';
|
|
4
|
+
/**
|
|
5
|
+
* Global state for Next.js Pages Router adapter.
|
|
6
|
+
*
|
|
7
|
+
* Next.js dev mode with HMR creates multiple module instances, causing createScenarist()
|
|
8
|
+
* to be called multiple times. These globals ensure:
|
|
9
|
+
* 1. MSW server.listen() is only called once
|
|
10
|
+
* 2. All instances share the same scenario registry and store
|
|
11
|
+
* 3. The same Scenarist instance is returned when called multiple times
|
|
12
|
+
*
|
|
13
|
+
* IMPORTANT: App Router and Pages Router use separate global keys (_pages suffix) to enable isolation.
|
|
14
|
+
* This allows a single Next.js application to use both routers simultaneously with
|
|
15
|
+
* independent scenario states. For example, App Router pages can be on scenario A
|
|
16
|
+
* while Pages Router pages are on scenario B, enabling gradual migration patterns.
|
|
17
|
+
*
|
|
18
|
+
* If shared globals were used, switching scenarios in Pages Router would affect App
|
|
19
|
+
* Router and vice versa, breaking test isolation when both routers are present.
|
|
20
|
+
*/
|
|
21
|
+
declare global {
|
|
22
|
+
var __scenarist_msw_started_pages: boolean | undefined;
|
|
23
|
+
var __scenarist_registry_pages: ScenarioRegistry | undefined;
|
|
24
|
+
var __scenarist_store_pages: ScenarioStore | undefined;
|
|
25
|
+
var __scenarist_instance_pages: PagesScenarist | undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Type for request objects that have headers.
|
|
29
|
+
* Compatible with both NextApiRequest and GetServerSidePropsContext.req
|
|
30
|
+
*/
|
|
31
|
+
type RequestWithHeaders = {
|
|
32
|
+
headers: IncomingMessage['headers'];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Next.js Pages Router adapter options.
|
|
36
|
+
*
|
|
37
|
+
* Extends BaseAdapterOptions to ensure consistency across all adapters.
|
|
38
|
+
*/
|
|
39
|
+
export type PagesAdapterOptions = BaseAdapterOptions;
|
|
40
|
+
/**
|
|
41
|
+
* Next.js Pages Router adapter instance.
|
|
42
|
+
*
|
|
43
|
+
* Provides MSW server lifecycle management and scenario endpoint factory.
|
|
44
|
+
* Unlike Express adapter, doesn't provide middleware (Next.js doesn't have global middleware for Pages Router).
|
|
45
|
+
*/
|
|
46
|
+
export type PagesScenarist = Omit<ScenaristAdapter<never>, 'middleware'> & {
|
|
47
|
+
/**
|
|
48
|
+
* Create scenario endpoint handler for use in pages/api/__scenario__.ts
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // pages/api/__scenario__.ts
|
|
53
|
+
* import { scenarist } from '../../lib/scenarist';
|
|
54
|
+
*
|
|
55
|
+
* export default scenarist?.createScenarioEndpoint();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
createScenarioEndpoint: () => ReturnType<typeof createScenarioEndpoint>;
|
|
59
|
+
/**
|
|
60
|
+
* Extract Scenarist infrastructure headers from the request.
|
|
61
|
+
*
|
|
62
|
+
* This helper respects the configured test ID header name and default test ID,
|
|
63
|
+
* ensuring headers are forwarded correctly when making external API calls.
|
|
64
|
+
*
|
|
65
|
+
* Works with both NextApiRequest (API routes) and GetServerSidePropsContext.req (SSR).
|
|
66
|
+
*
|
|
67
|
+
* @param req - Request object with headers
|
|
68
|
+
* @returns Object with single entry: configured test ID header name → value from request or default
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* // API Route
|
|
73
|
+
* export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
74
|
+
* const response = await fetch('http://localhost:3001/products', {
|
|
75
|
+
* headers: scenarist.getHeaders(req),
|
|
76
|
+
* });
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* // getServerSideProps
|
|
80
|
+
* export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
81
|
+
* const response = await fetch('http://localhost:3001/products', {
|
|
82
|
+
* headers: scenarist.getHeaders(context.req),
|
|
83
|
+
* });
|
|
84
|
+
* };
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
getHeaders: (req: RequestWithHeaders) => Record<string, string>;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Create a Scenarist instance for Next.js Pages Router (implementation).
|
|
91
|
+
*
|
|
92
|
+
* This is the actual implementation that wires everything automatically:
|
|
93
|
+
* - MSW server with dynamic handler
|
|
94
|
+
* - Scenario manager
|
|
95
|
+
* - State manager and sequence tracker
|
|
96
|
+
* - Response selector
|
|
97
|
+
*
|
|
98
|
+
* Unlike Express adapter, this doesn't return middleware. Instead, use `createScenarioEndpoint()`
|
|
99
|
+
* to create the handler for your pages/api/__scenario__.ts file.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // lib/scenarist.ts
|
|
104
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/pages';
|
|
105
|
+
* import { defaultScenario } from './scenarios';
|
|
106
|
+
*
|
|
107
|
+
* export const scenarist = createScenarist({
|
|
108
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
109
|
+
* defaultScenario,
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* // pages/api/__scenario__.ts
|
|
113
|
+
* export default scenarist?.createScenarioEndpoint();
|
|
114
|
+
*
|
|
115
|
+
* // tests/setup.ts
|
|
116
|
+
* if (scenarist) {
|
|
117
|
+
* beforeAll(() => scenarist.start());
|
|
118
|
+
* afterAll(() => scenarist.stop());
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare const createScenaristImpl: (options: PagesAdapterOptions) => PagesScenarist;
|
|
123
|
+
export {};
|
|
124
|
+
//# sourceMappingURL=impl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"impl.d.ts","sourceRoot":"","sources":["../../src/pages/impl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAG7G,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,CAAC,MAAM,CAAC;IAEb,IAAI,6BAA6B,EAAE,OAAO,GAAG,SAAS,CAAC;IAEvD,IAAI,0BAA0B,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAE7D,IAAI,uBAAuB,EAAE,aAAa,GAAG,SAAS,CAAC;IAEvD,IAAI,0BAA0B,EAAE,cAAc,GAAG,SAAS,CAAC;CAC5D;AAED;;;GAGG;AACH,KAAK,kBAAkB,GAAG;IACxB,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;CACrC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAErD;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,GAAG;IACzE;;;;;;;;;;OAUG;IACH,sBAAsB,EAAE,MAAM,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;IAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,UAAU,EAAE,CAAC,GAAG,EAAE,kBAAkB,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjE,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,mBAAmB,KAC3B,cA8DF,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 Pages 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 pages/api/__scenario__.ts file.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // lib/scenarist.ts
|
|
19
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/pages';
|
|
20
|
+
* import { defaultScenario } from './scenarios';
|
|
21
|
+
*
|
|
22
|
+
* export const scenarist = createScenarist({
|
|
23
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
24
|
+
* defaultScenario,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // pages/api/__scenario__.ts
|
|
28
|
+
* export default scenarist?.createScenarioEndpoint();
|
|
29
|
+
*
|
|
30
|
+
* // tests/setup.ts
|
|
31
|
+
* if (scenarist) {
|
|
32
|
+
* beforeAll(() => scenarist.start());
|
|
33
|
+
* afterAll(() => scenarist.stop());
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export const createScenaristImpl = (options) => {
|
|
38
|
+
// Singleton guard - return existing instance if already created
|
|
39
|
+
// This prevents duplicate scenario registration errors when Next.js creates multiple module instances
|
|
40
|
+
if (global.__scenarist_instance_pages) {
|
|
41
|
+
return global.__scenarist_instance_pages;
|
|
42
|
+
}
|
|
43
|
+
// Create or reuse global singleton stores for Next.js
|
|
44
|
+
// This ensures all module instances share the same scenario state
|
|
45
|
+
if (!global.__scenarist_registry_pages) {
|
|
46
|
+
global.__scenarist_registry_pages = new InMemoryScenarioRegistry();
|
|
47
|
+
}
|
|
48
|
+
if (!global.__scenarist_store_pages) {
|
|
49
|
+
global.__scenarist_store_pages = new InMemoryScenarioStore();
|
|
50
|
+
}
|
|
51
|
+
// Inject global singletons into base setup
|
|
52
|
+
const { config, manager, server, currentTestId } = createScenaristBase({
|
|
53
|
+
...options,
|
|
54
|
+
registry: global.__scenarist_registry_pages,
|
|
55
|
+
store: global.__scenarist_store_pages,
|
|
56
|
+
});
|
|
57
|
+
const instance = {
|
|
58
|
+
config,
|
|
59
|
+
switchScenario: (testId, scenarioId) => {
|
|
60
|
+
currentTestId.value = testId; // Update for MSW handler
|
|
61
|
+
return manager.switchScenario(testId, scenarioId);
|
|
62
|
+
},
|
|
63
|
+
getActiveScenario: (testId) => manager.getActiveScenario(testId),
|
|
64
|
+
getScenarioById: (scenarioId) => manager.getScenarioById(scenarioId),
|
|
65
|
+
listScenarios: () => manager.listScenarios(),
|
|
66
|
+
clearScenario: (testId) => manager.clearScenario(testId),
|
|
67
|
+
createScenarioEndpoint: () => createScenarioEndpoint(manager, config),
|
|
68
|
+
getHeaders: (req) => {
|
|
69
|
+
const headerValue = req.headers[SCENARIST_TEST_ID_HEADER];
|
|
70
|
+
const testId = (Array.isArray(headerValue) ? headerValue[0] : headerValue) || config.defaultTestId;
|
|
71
|
+
return {
|
|
72
|
+
[SCENARIST_TEST_ID_HEADER]: testId,
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
start: () => {
|
|
76
|
+
// Singleton guard - prevents duplicate MSW initialization
|
|
77
|
+
// Next.js dev mode with HMR creates multiple module instances, so this ensures
|
|
78
|
+
// server.listen() is only called once across all instances
|
|
79
|
+
if (global.__scenarist_msw_started_pages) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
server.listen();
|
|
83
|
+
// Mark as started
|
|
84
|
+
global.__scenarist_msw_started_pages = true;
|
|
85
|
+
},
|
|
86
|
+
stop: async () => server.close(),
|
|
87
|
+
};
|
|
88
|
+
// Store instance in global for singleton pattern
|
|
89
|
+
global.__scenarist_instance_pages = instance;
|
|
90
|
+
return instance;
|
|
91
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Pages Router adapter for Scenarist.
|
|
3
|
+
*
|
|
4
|
+
* Provides MSW-based scenario management for Next.js applications using Pages Router.
|
|
5
|
+
*
|
|
6
|
+
* @module @scenarist/nextjs-adapter/pages
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // lib/scenarist.ts
|
|
11
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/pages';
|
|
12
|
+
*
|
|
13
|
+
* export const scenarist = createScenarist({
|
|
14
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
15
|
+
* defaultScenario: myDefaultScenario,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // pages/api/__scenario__.ts
|
|
19
|
+
* import { scenarist } from '../../lib/scenarist';
|
|
20
|
+
*
|
|
21
|
+
* export default scenarist.createScenarioEndpoint();
|
|
22
|
+
*
|
|
23
|
+
* // tests/setup.ts
|
|
24
|
+
* import { scenarist } from '../lib/scenarist';
|
|
25
|
+
*
|
|
26
|
+
* beforeAll(() => scenarist.start());
|
|
27
|
+
* afterAll(() => scenarist.stop());
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export { createScenarist } from './setup.js';
|
|
31
|
+
export { PagesRequestContext } from './context.js';
|
|
32
|
+
export { createScenarioEndpoint } from './endpoints.js';
|
|
33
|
+
export { getScenaristHeaders } from './helpers.js';
|
|
34
|
+
export type { PagesAdapterOptions, PagesScenarist } from './setup.js';
|
|
35
|
+
export { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
36
|
+
export type { ScenaristScenario, ScenaristMock, ScenaristResponse, ScenaristSequence, ScenaristMatch, ScenaristCaptureConfig, ScenaristScenarios, ScenaristConfig, ScenaristResult, } from '@scenarist/core';
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pages/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,YAAY,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAGtE,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,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Pages Router adapter for Scenarist.
|
|
3
|
+
*
|
|
4
|
+
* Provides MSW-based scenario management for Next.js applications using Pages Router.
|
|
5
|
+
*
|
|
6
|
+
* @module @scenarist/nextjs-adapter/pages
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // lib/scenarist.ts
|
|
11
|
+
* import { createScenarist } from '@scenarist/nextjs-adapter/pages';
|
|
12
|
+
*
|
|
13
|
+
* export const scenarist = createScenarist({
|
|
14
|
+
* enabled: process.env.NODE_ENV === 'development',
|
|
15
|
+
* defaultScenario: myDefaultScenario,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // pages/api/__scenario__.ts
|
|
19
|
+
* import { scenarist } from '../../lib/scenarist';
|
|
20
|
+
*
|
|
21
|
+
* export default scenarist.createScenarioEndpoint();
|
|
22
|
+
*
|
|
23
|
+
* // tests/setup.ts
|
|
24
|
+
* import { scenarist } from '../lib/scenarist';
|
|
25
|
+
*
|
|
26
|
+
* beforeAll(() => scenarist.start());
|
|
27
|
+
* afterAll(() => scenarist.stop());
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export { createScenarist } from './setup.js';
|
|
31
|
+
export { PagesRequestContext } from './context.js';
|
|
32
|
+
export { createScenarioEndpoint } from './endpoints.js';
|
|
33
|
+
export { getScenaristHeaders } from './helpers.js';
|
|
34
|
+
// Re-export core types and constants for user convenience (users should only install this adapter)
|
|
35
|
+
export { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type { PagesAdapterOptions, PagesScenarist } 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) so that application code
|
|
11
|
+
* can import and use them without production guards.
|
|
12
|
+
*/
|
|
13
|
+
export declare const createScenarist: (_options: import("./setup.js").PagesAdapterOptions) => import("./setup.js").PagesScenarist | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Production stub: Returns empty object (no test headers needed in production)
|
|
16
|
+
*/
|
|
17
|
+
export declare function getScenaristHeaders(_req: import('next').NextApiRequest): Record<string, string>;
|
|
18
|
+
//# sourceMappingURL=production.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"production.d.ts","sourceRoot":"","sources":["../../src/pages/production.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEtE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,OAAO,YAAY,EAAE,mBAAmB,KACjD,OAAO,YAAY,EAAE,cAAc,GAAG,SAExC,CAAC;AAEF;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,MAAM,EAAE,cAAc,GAClC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAExB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
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) so that application code
|
|
10
|
+
* 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
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Entry Point Tests - Next.js Pages 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
|
+
* "./pages": {
|
|
18
|
+
* "production": "./dist/pages/production.js",
|
|
19
|
+
* "default": "./dist/pages/index.js"
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* This means:
|
|
26
|
+
* - In development/test: `import { createScenarist } from '@scenarist/nextjs-adapter/pages'`
|
|
27
|
+
* resolves to `dist/pages/index.js` (full Scenarist implementation)
|
|
28
|
+
* - In production: Same import resolves to `dist/pages/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
|
+
* // API Route - works in ALL environments
|
|
51
|
+
* import { getScenaristHeaders } from '@scenarist/nextjs-adapter/pages';
|
|
52
|
+
* import type { NextApiRequest, NextApiResponse } from 'next';
|
|
53
|
+
*
|
|
54
|
+
* export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
55
|
+
* const response = await fetch('http://api.example.com/data', {
|
|
56
|
+
* headers: {
|
|
57
|
+
* ...getScenaristHeaders(req), // ✅ Safe: {} in production
|
|
58
|
+
* 'x-api-key': 'abc123',
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* res.json(await response.json());
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* In production, `getScenaristHeaders()` returns `{}`, so spreading it is a no-op.
|
|
67
|
+
* No runtime errors, no guards needed, same code everywhere.
|
|
68
|
+
*
|
|
69
|
+
* ## Pages Router vs App Router
|
|
70
|
+
*
|
|
71
|
+
* The Pages Router adapter has simpler helper functions than App Router because:
|
|
72
|
+
* - Pages Router only has API Routes (no Server Components)
|
|
73
|
+
* - API Routes use NextApiRequest (not Web Request or ReadonlyHeaders)
|
|
74
|
+
* - Only one helper needed: getScenaristHeaders(req: NextApiRequest)
|
|
75
|
+
*
|
|
76
|
+
* App Router needs more helpers to support:
|
|
77
|
+
* - Route Handlers (Web Request API)
|
|
78
|
+
* - Server Components (ReadonlyHeaders from next/headers)
|
|
79
|
+
* - Both getHeaders() and getTestId() variants for each context
|
|
80
|
+
*
|
|
81
|
+
* ## Related Documentation
|
|
82
|
+
*
|
|
83
|
+
* **Core Feature (Node.js Conditional Exports):**
|
|
84
|
+
* - https://nodejs.org/api/packages.html#conditional-exports
|
|
85
|
+
* Defines how package.json "exports" field works with conditions like "production"
|
|
86
|
+
* - https://nodejs.org/api/packages.html#exports
|
|
87
|
+
* Complete specification for package.json exports field
|
|
88
|
+
*
|
|
89
|
+
* **Next.js Build Optimizations:**
|
|
90
|
+
* - https://nextjs.org/blog/next-14-2
|
|
91
|
+
* Next.js 14.2 tree-shaking improvements (see "Improved Tree Shaking" section)
|
|
92
|
+
* - https://nextjs.org/docs/app/guides/package-bundling
|
|
93
|
+
* Next.js package bundling and optimization strategies
|
|
94
|
+
*
|
|
95
|
+
* **Next.js Pages Router:**
|
|
96
|
+
* - https://nextjs.org/docs/pages/building-your-application/routing/api-routes
|
|
97
|
+
* Pages Router API route handler pattern (NextApiRequest/NextApiResponse)
|
|
98
|
+
*
|
|
99
|
+
* **Bundler Documentation:**
|
|
100
|
+
* - https://webpack.js.org/configuration/mode/#mode-production
|
|
101
|
+
* Webpack production mode (sets NODE_ENV=production, enables conditional exports)
|
|
102
|
+
* - https://turbo.build/pack/docs
|
|
103
|
+
* Turbopack (Next.js's new bundler, also respects conditional exports)
|
|
104
|
+
*
|
|
105
|
+
* **Note:** Next.js doesn't document conditional exports specifically because it's a
|
|
106
|
+
* Node.js feature that Next.js (via Webpack/Turbopack) automatically respects during
|
|
107
|
+
* production builds. The key is that NODE_ENV=production triggers the "production"
|
|
108
|
+
* export path in package.json.
|
|
109
|
+
*
|
|
110
|
+
* ## Testing Strategy
|
|
111
|
+
*
|
|
112
|
+
* These tests verify:
|
|
113
|
+
* 1. All stub functions return safe, expected defaults
|
|
114
|
+
* 2. createScenarist returns undefined (no Scenarist instance in production)
|
|
115
|
+
* 3. Helper functions can be called without errors
|
|
116
|
+
* 4. No imports from test dependencies (tree-shaking verification)
|
|
117
|
+
*/
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=production.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"production.test.d.ts","sourceRoot":"","sources":["../../src/pages/production.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoHG"}
|