@scenarist/nextjs-adapter 0.3.0 → 0.3.2
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 +117 -0
- package/dist/pages/context.d.ts.map +1 -1
- package/dist/pages/context.js +1 -10
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1235,6 +1235,123 @@ const scenarist = createScenarist({
|
|
|
1235
1235
|
});
|
|
1236
1236
|
```
|
|
1237
1237
|
|
|
1238
|
+
## Logging & Debugging
|
|
1239
|
+
|
|
1240
|
+
Scenarist includes a flexible logging system for debugging scenario matching, state management, and request handling. Logging is **disabled by default** and must be explicitly enabled. For comprehensive documentation including log categories, custom loggers, and Vitest configuration, see the [full logging guide](https://scenarist.io/reference/logging).
|
|
1241
|
+
|
|
1242
|
+
### Quick Start
|
|
1243
|
+
|
|
1244
|
+
```typescript
|
|
1245
|
+
// App Router
|
|
1246
|
+
import {
|
|
1247
|
+
createScenarist,
|
|
1248
|
+
createConsoleLogger,
|
|
1249
|
+
} from "@scenarist/nextjs-adapter/app";
|
|
1250
|
+
|
|
1251
|
+
// Pages Router
|
|
1252
|
+
import {
|
|
1253
|
+
createScenarist,
|
|
1254
|
+
createConsoleLogger,
|
|
1255
|
+
} from "@scenarist/nextjs-adapter/pages";
|
|
1256
|
+
|
|
1257
|
+
export const scenarist = createScenarist({
|
|
1258
|
+
enabled: process.env.NODE_ENV === "test",
|
|
1259
|
+
scenarios,
|
|
1260
|
+
|
|
1261
|
+
// Enable logging with pretty format
|
|
1262
|
+
logger: createConsoleLogger({ level: "info", format: "pretty" }),
|
|
1263
|
+
});
|
|
1264
|
+
```
|
|
1265
|
+
|
|
1266
|
+
### Environment Variable Pattern
|
|
1267
|
+
|
|
1268
|
+
For easy toggling without code changes:
|
|
1269
|
+
|
|
1270
|
+
```typescript
|
|
1271
|
+
import {
|
|
1272
|
+
createScenarist,
|
|
1273
|
+
createConsoleLogger,
|
|
1274
|
+
noOpLogger,
|
|
1275
|
+
type LogLevel,
|
|
1276
|
+
type LogFormat,
|
|
1277
|
+
} from "@scenarist/nextjs-adapter/app";
|
|
1278
|
+
|
|
1279
|
+
// Type-safe environment variable parsing
|
|
1280
|
+
const LOG_LEVELS: ReadonlyArray<Exclude<LogLevel, "silent">> = [
|
|
1281
|
+
"error",
|
|
1282
|
+
"warn",
|
|
1283
|
+
"info",
|
|
1284
|
+
"debug",
|
|
1285
|
+
"trace",
|
|
1286
|
+
];
|
|
1287
|
+
const LOG_FORMATS: ReadonlyArray<LogFormat> = ["pretty", "json"];
|
|
1288
|
+
|
|
1289
|
+
const parseLogLevel = (
|
|
1290
|
+
value: string | undefined,
|
|
1291
|
+
): Exclude<LogLevel, "silent"> =>
|
|
1292
|
+
LOG_LEVELS.includes(value as Exclude<LogLevel, "silent">)
|
|
1293
|
+
? (value as Exclude<LogLevel, "silent">)
|
|
1294
|
+
: "info";
|
|
1295
|
+
|
|
1296
|
+
const parseLogFormat = (value: string | undefined): LogFormat =>
|
|
1297
|
+
LOG_FORMATS.includes(value as LogFormat) ? (value as LogFormat) : "pretty";
|
|
1298
|
+
|
|
1299
|
+
export const scenarist = createScenarist({
|
|
1300
|
+
enabled: process.env.NODE_ENV === "test",
|
|
1301
|
+
scenarios,
|
|
1302
|
+
|
|
1303
|
+
// Enable via SCENARIST_LOG=1 environment variable
|
|
1304
|
+
logger: process.env.SCENARIST_LOG
|
|
1305
|
+
? createConsoleLogger({
|
|
1306
|
+
level: parseLogLevel(process.env.SCENARIST_LOG_LEVEL),
|
|
1307
|
+
format: parseLogFormat(process.env.SCENARIST_LOG_FORMAT),
|
|
1308
|
+
})
|
|
1309
|
+
: noOpLogger,
|
|
1310
|
+
});
|
|
1311
|
+
```
|
|
1312
|
+
|
|
1313
|
+
Then run tests with logging:
|
|
1314
|
+
|
|
1315
|
+
```bash
|
|
1316
|
+
# Enable info-level logging
|
|
1317
|
+
SCENARIST_LOG=1 pnpm test
|
|
1318
|
+
|
|
1319
|
+
# Enable debug-level logging for match troubleshooting
|
|
1320
|
+
SCENARIST_LOG=1 SCENARIST_LOG_LEVEL=debug pnpm test
|
|
1321
|
+
```
|
|
1322
|
+
|
|
1323
|
+
> **Note:** `SCENARIST_LOG` is a convention for your code, not something Scenarist reads automatically. You must explicitly pass a `logger` to `createScenarist()` as shown above.
|
|
1324
|
+
|
|
1325
|
+
### Log Levels
|
|
1326
|
+
|
|
1327
|
+
| Level | Description | Use Case |
|
|
1328
|
+
| ------- | ----------------- | --------------------------------------------- |
|
|
1329
|
+
| `error` | Critical failures | Scenario not found, invalid config |
|
|
1330
|
+
| `warn` | Potential issues | No mock matched, sequence exhausted |
|
|
1331
|
+
| `info` | Key events | Scenario switched, mock selected |
|
|
1332
|
+
| `debug` | Decision logic | Match criteria evaluation, specificity scores |
|
|
1333
|
+
| `trace` | Verbose details | Request/response bodies, template replacement |
|
|
1334
|
+
|
|
1335
|
+
### Sample Output
|
|
1336
|
+
|
|
1337
|
+
**Pretty format** (default) - human-readable with emojis and colors:
|
|
1338
|
+
|
|
1339
|
+
```
|
|
1340
|
+
12:34:56.789 INF 🎬 [test-user-login] scenario | scenario_switched scenarioId=premium-user
|
|
1341
|
+
12:34:56.801 INF 🎯 [test-user-login] matching | mock_selected mockIndex=2 specificity=5
|
|
1342
|
+
12:34:56.810 INF 💾 [test-user-login] state | state_captured key=userId value=user-123
|
|
1343
|
+
12:34:56.815 WRN 🎯 [test-user-login] matching | mock_no_match url=/api/unknown
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
**JSON format** - for log aggregation tools (Datadog, Splunk, etc.):
|
|
1347
|
+
|
|
1348
|
+
```json
|
|
1349
|
+
{"level":"info","category":"scenario","message":"scenario_switched","testId":"test-user-login","scenarioId":"premium-user","timestamp":1732650896789}
|
|
1350
|
+
{"level":"info","category":"matching","message":"mock_selected","testId":"test-user-login","data":{"mockIndex":2,"specificity":5},"timestamp":1732650896801}
|
|
1351
|
+
```
|
|
1352
|
+
|
|
1353
|
+
For more details including log categories, custom loggers, and Vitest configuration, see the [full logging documentation](https://scenarist.io/reference/logging).
|
|
1354
|
+
|
|
1238
1355
|
## Troubleshooting
|
|
1239
1356
|
|
|
1240
1357
|
### Scenarios switch but requests aren't mocked
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/pages/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAC;AAEzB;;;;;GAKG;AACH,qBAAa,mBAAoB,YAAW,cAAc;IAEtD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,GAAG,EAAE,cAAc,EACnB,MAAM,EAAE,eAAe;IAG1C,SAAS,IAAI,MAAM;IAcnB,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAI3D,WAAW,IAAI,MAAM;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/pages/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAC;AAEzB;;;;;GAKG;AACH,qBAAa,mBAAoB,YAAW,cAAc;IAEtD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,GAAG,EAAE,cAAc,EACnB,MAAM,EAAE,eAAe;IAG1C,SAAS,IAAI,MAAM;IAcnB,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAI3D,WAAW,IAAI,MAAM;CAItB"}
|
package/dist/pages/context.js
CHANGED
|
@@ -27,15 +27,6 @@ export class PagesRequestContext {
|
|
|
27
27
|
}
|
|
28
28
|
getHostname() {
|
|
29
29
|
const host = this.req.headers.host;
|
|
30
|
-
|
|
31
|
-
return "";
|
|
32
|
-
}
|
|
33
|
-
// After null check, host is string | string[]
|
|
34
|
-
if (typeof host === "string") {
|
|
35
|
-
return host;
|
|
36
|
-
}
|
|
37
|
-
// host must be string[] at this point (type assertion needed due to TS control flow limitation)
|
|
38
|
-
const hostArray = host;
|
|
39
|
-
return hostArray.length > 0 ? (hostArray[0] ?? "") : "";
|
|
30
|
+
return host ?? "";
|
|
40
31
|
}
|
|
41
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scenarist/nextjs-adapter",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Next.js adapter for Scenarist (Pages Router and App Router)",
|
|
5
5
|
"author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -59,8 +59,8 @@
|
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"zod": "^4.1.13",
|
|
62
|
-
"@scenarist/core": "0.3.
|
|
63
|
-
"@scenarist/msw-adapter": "0.3.
|
|
62
|
+
"@scenarist/core": "0.3.2",
|
|
63
|
+
"@scenarist/msw-adapter": "0.3.2"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
66
|
"msw": "^2.0.0",
|