@scenarist/express-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.
Files changed (2) hide show
  1. package/README.md +110 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1018,6 +1018,116 @@ The script checks that MSW-specific implementation patterns (`setupWorker`, `Htt
1018
1018
  - If you're deploying unbundled code: No action needed ✅
1019
1019
  - If you're bundling: Add the one-line bundler configuration for optimal bundle size
1020
1020
 
1021
+ ## Logging & Debugging
1022
+
1023
+ 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).
1024
+
1025
+ ### Quick Start
1026
+
1027
+ ```typescript
1028
+ import {
1029
+ createScenarist,
1030
+ createConsoleLogger,
1031
+ } from "@scenarist/express-adapter";
1032
+
1033
+ const scenarist = createScenarist({
1034
+ enabled: process.env.NODE_ENV === "test",
1035
+ scenarios,
1036
+
1037
+ // Enable logging with pretty format
1038
+ logger: createConsoleLogger({ level: "info", format: "pretty" }),
1039
+ });
1040
+ ```
1041
+
1042
+ ### Environment Variable Pattern
1043
+
1044
+ For easy toggling without code changes:
1045
+
1046
+ ```typescript
1047
+ import {
1048
+ createScenarist,
1049
+ createConsoleLogger,
1050
+ noOpLogger,
1051
+ type LogLevel,
1052
+ type LogFormat,
1053
+ } from "@scenarist/express-adapter";
1054
+
1055
+ // Type-safe environment variable parsing
1056
+ const LOG_LEVELS: ReadonlyArray<Exclude<LogLevel, "silent">> = [
1057
+ "error",
1058
+ "warn",
1059
+ "info",
1060
+ "debug",
1061
+ "trace",
1062
+ ];
1063
+ const LOG_FORMATS: ReadonlyArray<LogFormat> = ["pretty", "json"];
1064
+
1065
+ const parseLogLevel = (
1066
+ value: string | undefined,
1067
+ ): Exclude<LogLevel, "silent"> =>
1068
+ LOG_LEVELS.includes(value as Exclude<LogLevel, "silent">)
1069
+ ? (value as Exclude<LogLevel, "silent">)
1070
+ : "info";
1071
+
1072
+ const parseLogFormat = (value: string | undefined): LogFormat =>
1073
+ LOG_FORMATS.includes(value as LogFormat) ? (value as LogFormat) : "pretty";
1074
+
1075
+ const scenarist = createScenarist({
1076
+ enabled: process.env.NODE_ENV === "test",
1077
+ scenarios,
1078
+
1079
+ // Enable via SCENARIST_LOG=1 environment variable
1080
+ logger: process.env.SCENARIST_LOG
1081
+ ? createConsoleLogger({
1082
+ level: parseLogLevel(process.env.SCENARIST_LOG_LEVEL),
1083
+ format: parseLogFormat(process.env.SCENARIST_LOG_FORMAT),
1084
+ })
1085
+ : noOpLogger,
1086
+ });
1087
+ ```
1088
+
1089
+ Then run tests with logging:
1090
+
1091
+ ```bash
1092
+ # Enable info-level logging
1093
+ SCENARIST_LOG=1 pnpm test
1094
+
1095
+ # Enable debug-level logging for match troubleshooting
1096
+ SCENARIST_LOG=1 SCENARIST_LOG_LEVEL=debug pnpm test
1097
+ ```
1098
+
1099
+ > **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.
1100
+
1101
+ ### Log Levels
1102
+
1103
+ | Level | Description | Use Case |
1104
+ | ------- | ----------------- | --------------------------------------------- |
1105
+ | `error` | Critical failures | Scenario not found, invalid config |
1106
+ | `warn` | Potential issues | No mock matched, sequence exhausted |
1107
+ | `info` | Key events | Scenario switched, mock selected |
1108
+ | `debug` | Decision logic | Match criteria evaluation, specificity scores |
1109
+ | `trace` | Verbose details | Request/response bodies, template replacement |
1110
+
1111
+ ### Sample Output
1112
+
1113
+ **Pretty format** (default) - human-readable with emojis and colors:
1114
+
1115
+ ```
1116
+ 12:34:56.789 INF 🎬 [test-user-login] scenario | scenario_switched scenarioId=premium-user
1117
+ 12:34:56.801 INF 🎯 [test-user-login] matching | mock_selected mockIndex=2 specificity=5
1118
+ 12:34:56.810 INF 💾 [test-user-login] state | state_captured key=userId value=user-123
1119
+ 12:34:56.815 WRN 🎯 [test-user-login] matching | mock_no_match url=/api/unknown
1120
+ ```
1121
+
1122
+ **JSON format** - for log aggregation tools (Datadog, Splunk, etc.):
1123
+
1124
+ ```json
1125
+ {"level":"info","category":"scenario","message":"scenario_switched","testId":"test-user-login","scenarioId":"premium-user","timestamp":1732650896789}
1126
+ {"level":"info","category":"matching","message":"mock_selected","testId":"test-user-login","data":{"mockIndex":2,"specificity":5},"timestamp":1732650896801}
1127
+ ```
1128
+
1129
+ For more details including log categories, custom loggers, and Vitest configuration, see the [full logging documentation](https://scenarist.io/reference/logging).
1130
+
1021
1131
  ## Troubleshooting
1022
1132
 
1023
1133
  ### Scenarios switch but requests aren't mocked
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenarist/express-adapter",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Express middleware adapter for Scenarist",
5
5
  "author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
6
6
  "license": "MIT",
@@ -48,8 +48,8 @@
48
48
  ],
49
49
  "dependencies": {
50
50
  "zod": "^4.1.13",
51
- "@scenarist/core": "0.3.0",
52
- "@scenarist/msw-adapter": "0.3.0"
51
+ "@scenarist/msw-adapter": "0.3.2",
52
+ "@scenarist/core": "0.3.2"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "express": "^4.18.0 || ^5.0.0",