@scenarist/playwright-helpers 0.1.7 → 0.1.8
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 +173 -118
- package/dist/fixtures.d.ts +4 -4
- package/dist/fixtures.d.ts.map +1 -1
- package/dist/fixtures.js +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/switch-scenario.d.ts +1 -1
- package/dist/switch-scenario.js +4 -4
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -9,12 +9,12 @@ Playwright test helpers for [Scenarist](https://scenarist.io) scenario managemen
|
|
|
9
9
|
|
|
10
10
|
**Full documentation at [scenarist.io](https://scenarist.io)**
|
|
11
11
|
|
|
12
|
-
| Topic
|
|
13
|
-
|
|
14
|
-
| **Why Scenarist?**
|
|
12
|
+
| Topic | Link |
|
|
13
|
+
| -------------------------- | -------------------------------------------------------------------------------------------------- |
|
|
14
|
+
| **Why Scenarist?** | [scenarist.io/getting-started/why-scenarist](https://scenarist.io/getting-started/why-scenarist) |
|
|
15
15
|
| **Playwright Integration** | [scenarist.io/testing/playwright-integration](https://scenarist.io/testing/playwright-integration) |
|
|
16
|
-
| **Parallel Testing**
|
|
17
|
-
| **Testing Philosophy**
|
|
16
|
+
| **Parallel Testing** | [scenarist.io/testing/parallel-testing](https://scenarist.io/testing/parallel-testing) |
|
|
17
|
+
| **Testing Philosophy** | [scenarist.io/concepts/philosophy](https://scenarist.io/concepts/philosophy) |
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -32,6 +32,7 @@ yarn add -D @scenarist/playwright-helpers
|
|
|
32
32
|
## Quick Start (Recommended: Fixtures API)
|
|
33
33
|
|
|
34
34
|
The **fixtures API** is the recommended way to use Scenarist with Playwright. It provides:
|
|
35
|
+
|
|
35
36
|
- ✅ Guaranteed unique test IDs (no collisions, even with parallel execution)
|
|
36
37
|
- ✅ Configuration in one place (no repetition across tests)
|
|
37
38
|
- ✅ Clean composition with your existing fixtures
|
|
@@ -40,30 +41,44 @@ The **fixtures API** is the recommended way to use Scenarist with Playwright. It
|
|
|
40
41
|
### 1. Configure in `playwright.config.ts`
|
|
41
42
|
|
|
42
43
|
```typescript
|
|
43
|
-
import { defineConfig } from
|
|
44
|
-
import type { ScenaristOptions } from
|
|
44
|
+
import { defineConfig } from "@playwright/test";
|
|
45
|
+
import type { ScenaristOptions } from "@scenarist/playwright-helpers";
|
|
45
46
|
|
|
46
47
|
export default defineConfig<ScenaristOptions>({
|
|
47
48
|
use: {
|
|
48
|
-
baseURL:
|
|
49
|
-
scenaristEndpoint:
|
|
49
|
+
baseURL: "http://localhost:3000", // Standard Playwright config
|
|
50
|
+
scenaristEndpoint: "/api/__scenario__", // Scenarist-specific config
|
|
50
51
|
},
|
|
51
52
|
});
|
|
52
53
|
```
|
|
53
54
|
|
|
54
55
|
**Note:** The `<ScenaristOptions>` type parameter enables TypeScript to recognize `scenaristEndpoint` as a valid configuration option.
|
|
55
56
|
|
|
56
|
-
### 2.
|
|
57
|
+
### 2. Create a Fixtures File
|
|
58
|
+
|
|
59
|
+
Create `tests/fixtures.ts` with your scenarios:
|
|
57
60
|
|
|
58
61
|
```typescript
|
|
59
|
-
|
|
62
|
+
// tests/fixtures.ts
|
|
63
|
+
import { withScenarios, expect } from "@scenarist/playwright-helpers";
|
|
64
|
+
import { scenarios } from "../lib/scenarios"; // Your scenario definitions
|
|
65
|
+
|
|
66
|
+
export const test = withScenarios(scenarios);
|
|
67
|
+
export { expect };
|
|
68
|
+
```
|
|
60
69
|
|
|
61
|
-
|
|
70
|
+
### 3. Use in Tests
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// tests/my-test.spec.ts
|
|
74
|
+
import { test, expect } from "./fixtures"; // Import from YOUR fixtures file
|
|
75
|
+
|
|
76
|
+
test("premium user sees premium pricing", async ({ page, switchScenario }) => {
|
|
62
77
|
// Configuration read from playwright.config.ts - no repetition!
|
|
63
|
-
await switchScenario(page,
|
|
78
|
+
await switchScenario(page, "premiumUser"); // Type-safe! Autocomplete works
|
|
64
79
|
|
|
65
|
-
await page.goto(
|
|
66
|
-
await expect(page.getByRole(
|
|
80
|
+
await page.goto("/");
|
|
81
|
+
await expect(page.getByRole("heading", { name: "Premium" })).toBeVisible();
|
|
67
82
|
});
|
|
68
83
|
```
|
|
69
84
|
|
|
@@ -75,20 +90,22 @@ If your team already has custom Playwright fixtures, you can easily compose them
|
|
|
75
90
|
|
|
76
91
|
```typescript
|
|
77
92
|
// tests/fixtures.ts
|
|
78
|
-
import {
|
|
93
|
+
import { withScenarios, expect } from "@scenarist/playwright-helpers";
|
|
94
|
+
import { scenarios } from "../lib/scenarios";
|
|
95
|
+
import type { Page } from "@playwright/test";
|
|
79
96
|
|
|
80
97
|
type MyFixtures = {
|
|
81
98
|
authenticatedPage: Page;
|
|
82
99
|
database: Database;
|
|
83
100
|
};
|
|
84
101
|
|
|
85
|
-
export const test =
|
|
102
|
+
export const test = withScenarios(scenarios).extend<MyFixtures>({
|
|
86
103
|
authenticatedPage: async ({ page }, use) => {
|
|
87
104
|
// Your custom fixture logic
|
|
88
|
-
await page.goto(
|
|
89
|
-
await page.fill(
|
|
90
|
-
await page.fill(
|
|
91
|
-
await page.click(
|
|
105
|
+
await page.goto("/login");
|
|
106
|
+
await page.fill("[name=email]", "test@example.com");
|
|
107
|
+
await page.fill("[name=password]", "password");
|
|
108
|
+
await page.click("button[type=submit]");
|
|
92
109
|
await use(page);
|
|
93
110
|
},
|
|
94
111
|
|
|
@@ -99,54 +116,55 @@ export const test = scenaristTest.extend<MyFixtures>({
|
|
|
99
116
|
},
|
|
100
117
|
});
|
|
101
118
|
|
|
102
|
-
export { expect }
|
|
119
|
+
export { expect };
|
|
103
120
|
```
|
|
104
121
|
|
|
105
122
|
Now use your extended test object:
|
|
106
123
|
|
|
107
124
|
```typescript
|
|
108
125
|
// tests/my-test.spec.ts
|
|
109
|
-
import { test, expect } from
|
|
126
|
+
import { test, expect } from "./fixtures";
|
|
110
127
|
|
|
111
|
-
test(
|
|
128
|
+
test("authenticated premium user flow", async ({
|
|
129
|
+
authenticatedPage,
|
|
130
|
+
switchScenario,
|
|
131
|
+
database,
|
|
132
|
+
}) => {
|
|
112
133
|
// All fixtures available: yours + Scenarist's
|
|
113
|
-
await switchScenario(authenticatedPage,
|
|
114
|
-
await authenticatedPage.goto(
|
|
134
|
+
await switchScenario(authenticatedPage, "premiumUser");
|
|
135
|
+
await authenticatedPage.goto("/dashboard");
|
|
115
136
|
|
|
116
|
-
const user = await database.getUser(
|
|
117
|
-
expect(user.tier).toBe(
|
|
137
|
+
const user = await database.getUser("test@example.com");
|
|
138
|
+
expect(user.tier).toBe("premium");
|
|
118
139
|
});
|
|
119
140
|
```
|
|
120
141
|
|
|
121
|
-
## Type-Safe Scenario IDs (
|
|
142
|
+
## Type-Safe Scenario IDs (Automatic)
|
|
122
143
|
|
|
123
|
-
|
|
144
|
+
Type safety is **automatic** when you use `withScenarios(scenarios)`. The function infers scenario IDs from your scenarios object.
|
|
124
145
|
|
|
125
|
-
### 1. Define Scenarios
|
|
146
|
+
### 1. Define Scenarios
|
|
126
147
|
|
|
127
148
|
```typescript
|
|
128
149
|
// lib/scenarios.ts
|
|
129
|
-
import type {
|
|
150
|
+
import type { ScenaristScenarios } from '@scenarist/core';
|
|
130
151
|
|
|
131
152
|
export const scenarios = {
|
|
132
|
-
cartWithState: { id: 'cartWithState', name: 'Cart with State', ... },
|
|
133
|
-
premiumUser: { id: 'premiumUser', name: 'Premium User', ... },
|
|
134
|
-
standardUser: { id: 'standardUser', name: 'Standard User', ... },
|
|
153
|
+
cartWithState: { id: 'cartWithState', name: 'Cart with State', mocks: [...] },
|
|
154
|
+
premiumUser: { id: 'premiumUser', name: 'Premium User', mocks: [...] },
|
|
155
|
+
standardUser: { id: 'standardUser', name: 'Standard User', mocks: [...] },
|
|
135
156
|
} as const satisfies ScenaristScenarios;
|
|
136
|
-
|
|
137
|
-
// Derive type from actual scenarios (or use ScenarioIds<typeof scenarios>)
|
|
138
|
-
export type ScenarioId = keyof typeof scenarios;
|
|
139
157
|
```
|
|
140
158
|
|
|
141
159
|
### 2. Create Typed Test Object
|
|
142
160
|
|
|
143
161
|
```typescript
|
|
144
162
|
// tests/fixtures.ts
|
|
145
|
-
import {
|
|
146
|
-
import
|
|
163
|
+
import { withScenarios, expect } from "@scenarist/playwright-helpers";
|
|
164
|
+
import { scenarios } from "../lib/scenarios";
|
|
147
165
|
|
|
148
|
-
//
|
|
149
|
-
export const test =
|
|
166
|
+
// Type-safe test object - scenario IDs are inferred automatically!
|
|
167
|
+
export const test = withScenarios(scenarios);
|
|
150
168
|
export { expect };
|
|
151
169
|
```
|
|
152
170
|
|
|
@@ -154,17 +172,18 @@ export { expect };
|
|
|
154
172
|
|
|
155
173
|
```typescript
|
|
156
174
|
// tests/my-test.spec.ts
|
|
157
|
-
import { test, expect } from
|
|
175
|
+
import { test, expect } from "./fixtures";
|
|
158
176
|
|
|
159
|
-
test(
|
|
160
|
-
await switchScenario(page,
|
|
161
|
-
await switchScenario(page,
|
|
177
|
+
test("my test", async ({ page, switchScenario }) => {
|
|
178
|
+
await switchScenario(page, "cart"); // ❌ TypeScript error: not a valid scenario
|
|
179
|
+
await switchScenario(page, "cartWithState"); // ✅ Autocomplete works!
|
|
162
180
|
// ^
|
|
163
181
|
// Autocomplete shows all valid scenario IDs
|
|
164
182
|
});
|
|
165
183
|
```
|
|
166
184
|
|
|
167
185
|
**Benefits:**
|
|
186
|
+
|
|
168
187
|
- ✅ Autocomplete shows all valid scenario names
|
|
169
188
|
- ✅ TypeScript errors for typos or invalid scenarios
|
|
170
189
|
- ✅ Type stays in sync with actual scenarios (single source of truth)
|
|
@@ -175,13 +194,13 @@ test('my test', async ({ page, switchScenario }) => {
|
|
|
175
194
|
Most tests use the global config, but you can override for specific tests:
|
|
176
195
|
|
|
177
196
|
```typescript
|
|
178
|
-
test(
|
|
179
|
-
await switchScenario(page,
|
|
180
|
-
baseURL:
|
|
181
|
-
endpoint:
|
|
197
|
+
test("staging environment test", async ({ page, switchScenario }) => {
|
|
198
|
+
await switchScenario(page, "myScenario", {
|
|
199
|
+
baseURL: "https://staging.example.com", // Override for this test only
|
|
200
|
+
endpoint: "/api/custom-endpoint",
|
|
182
201
|
});
|
|
183
202
|
|
|
184
|
-
await page.goto(
|
|
203
|
+
await page.goto("/");
|
|
185
204
|
// Test against staging environment
|
|
186
205
|
});
|
|
187
206
|
```
|
|
@@ -191,19 +210,19 @@ test('staging environment test', async ({ page, switchScenario }) => {
|
|
|
191
210
|
For cases where you need manual control over test IDs or can't use fixtures:
|
|
192
211
|
|
|
193
212
|
```typescript
|
|
194
|
-
import { test, expect } from
|
|
195
|
-
import { switchScenario } from
|
|
213
|
+
import { test, expect } from "@playwright/test";
|
|
214
|
+
import { switchScenario } from "@scenarist/playwright-helpers";
|
|
196
215
|
|
|
197
|
-
test(
|
|
216
|
+
test("premium user scenario", async ({ page }) => {
|
|
198
217
|
// Switch to premium scenario (generates unique test ID, sets headers automatically)
|
|
199
|
-
await switchScenario(page,
|
|
200
|
-
baseURL:
|
|
201
|
-
endpoint:
|
|
218
|
+
await switchScenario(page, "premiumUser", {
|
|
219
|
+
baseURL: "http://localhost:3000",
|
|
220
|
+
endpoint: "/api/__scenario__",
|
|
202
221
|
});
|
|
203
222
|
|
|
204
223
|
// Navigate and test as premium user
|
|
205
|
-
await page.goto(
|
|
206
|
-
await expect(page.locator(
|
|
224
|
+
await page.goto("/");
|
|
225
|
+
await expect(page.locator(".premium-badge")).toBeVisible();
|
|
207
226
|
});
|
|
208
227
|
```
|
|
209
228
|
|
|
@@ -213,16 +232,17 @@ test('premium user scenario', async ({ page }) => {
|
|
|
213
232
|
|
|
214
233
|
```typescript
|
|
215
234
|
type SwitchScenarioOptions = {
|
|
216
|
-
readonly baseURL: string;
|
|
217
|
-
readonly endpoint?: string;
|
|
218
|
-
readonly testIdHeader?: string;
|
|
219
|
-
readonly variant?: string;
|
|
235
|
+
readonly baseURL: string; // Base URL of your application
|
|
236
|
+
readonly endpoint?: string; // Scenario endpoint path (default: '/__scenario__')
|
|
237
|
+
readonly testIdHeader?: string; // Test ID header name (default: 'x-scenarist-test-id')
|
|
238
|
+
readonly variant?: string; // Optional scenario variant
|
|
220
239
|
};
|
|
221
240
|
```
|
|
222
241
|
|
|
223
242
|
#### What it does
|
|
224
243
|
|
|
225
244
|
The `switchScenario` helper:
|
|
245
|
+
|
|
226
246
|
1. Generates a unique test ID (`test-{scenarioId}-{timestamp}`)
|
|
227
247
|
2. POSTs to the scenario endpoint with the test ID header
|
|
228
248
|
3. Verifies the scenario switch succeeded (200 response)
|
|
@@ -231,21 +251,26 @@ The `switchScenario` helper:
|
|
|
231
251
|
This reduces scenario switching from 9 lines of boilerplate to 2 lines:
|
|
232
252
|
|
|
233
253
|
**Without helper (9 lines):**
|
|
254
|
+
|
|
234
255
|
```typescript
|
|
235
256
|
const testId = `test-premium-${Date.now()}`;
|
|
236
|
-
const response = await page.request.post(
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
257
|
+
const response = await page.request.post(
|
|
258
|
+
"http://localhost:3000/api/__scenario__",
|
|
259
|
+
{
|
|
260
|
+
headers: { "x-scenarist-test-id": testId },
|
|
261
|
+
data: { scenario: "premiumUser" },
|
|
262
|
+
},
|
|
263
|
+
);
|
|
240
264
|
expect(response.status()).toBe(200);
|
|
241
|
-
await page.setExtraHTTPHeaders({
|
|
265
|
+
await page.setExtraHTTPHeaders({ "x-scenarist-test-id": testId });
|
|
242
266
|
```
|
|
243
267
|
|
|
244
268
|
**With helper (2 lines):**
|
|
269
|
+
|
|
245
270
|
```typescript
|
|
246
|
-
await switchScenario(page,
|
|
247
|
-
baseURL:
|
|
248
|
-
endpoint:
|
|
271
|
+
await switchScenario(page, "premiumUser", {
|
|
272
|
+
baseURL: "http://localhost:3000",
|
|
273
|
+
endpoint: "/api/__scenario__",
|
|
249
274
|
});
|
|
250
275
|
```
|
|
251
276
|
|
|
@@ -255,14 +280,24 @@ await switchScenario(page, 'premiumUser', {
|
|
|
255
280
|
|
|
256
281
|
### Fixtures API (Recommended)
|
|
257
282
|
|
|
258
|
-
#### `
|
|
283
|
+
#### `withScenarios(scenarios)`
|
|
284
|
+
|
|
285
|
+
Creates a type-safe Playwright test object with Scenarist fixtures.
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// tests/fixtures.ts
|
|
289
|
+
import { withScenarios, expect } from "@scenarist/playwright-helpers";
|
|
290
|
+
import { scenarios } from "../lib/scenarios";
|
|
259
291
|
|
|
260
|
-
|
|
292
|
+
export const test = withScenarios(scenarios);
|
|
293
|
+
export { expect };
|
|
294
|
+
```
|
|
261
295
|
|
|
262
296
|
```typescript
|
|
263
|
-
|
|
297
|
+
// tests/my-test.spec.ts
|
|
298
|
+
import { test, expect } from "./fixtures";
|
|
264
299
|
|
|
265
|
-
test(
|
|
300
|
+
test("my test", async ({ page, switchScenario, scenaristTestId }) => {
|
|
266
301
|
// Your test code
|
|
267
302
|
});
|
|
268
303
|
```
|
|
@@ -277,7 +312,7 @@ test('my test', async ({ page, switchScenario, scenaristTestId }) => {
|
|
|
277
312
|
Re-exported from `@playwright/test` for convenience:
|
|
278
313
|
|
|
279
314
|
```typescript
|
|
280
|
-
import {
|
|
315
|
+
import { withScenarios, expect } from "@scenarist/playwright-helpers";
|
|
281
316
|
```
|
|
282
317
|
|
|
283
318
|
#### Configuration Options
|
|
@@ -287,8 +322,8 @@ Set in `playwright.config.ts`:
|
|
|
287
322
|
```typescript
|
|
288
323
|
export default defineConfig({
|
|
289
324
|
use: {
|
|
290
|
-
baseURL:
|
|
291
|
-
scenaristEndpoint:
|
|
325
|
+
baseURL: "http://localhost:3000", // Standard Playwright (used by switchScenario)
|
|
326
|
+
scenaristEndpoint: "/api/__scenario__", // Scenarist endpoint path (default: '/api/__scenario__')
|
|
292
327
|
},
|
|
293
328
|
});
|
|
294
329
|
```
|
|
@@ -327,16 +362,17 @@ await switchScenario(page, scenarioId, options?)
|
|
|
327
362
|
For manual test ID control:
|
|
328
363
|
|
|
329
364
|
```typescript
|
|
330
|
-
import { switchScenario } from
|
|
365
|
+
import { switchScenario } from "@scenarist/playwright-helpers";
|
|
331
366
|
|
|
332
367
|
await switchScenario(page, scenarioId, {
|
|
333
|
-
baseURL:
|
|
334
|
-
endpoint:
|
|
335
|
-
testId:
|
|
368
|
+
baseURL: "http://localhost:3000",
|
|
369
|
+
endpoint: "/api/__scenario__",
|
|
370
|
+
testId: "my-custom-test-id", // Manual test ID
|
|
336
371
|
});
|
|
337
372
|
```
|
|
338
373
|
|
|
339
374
|
**Use this only when:**
|
|
375
|
+
|
|
340
376
|
- You need to share test IDs across multiple tests
|
|
341
377
|
- You're integrating with existing test infrastructure that provides test IDs
|
|
342
378
|
- You can't use Playwright fixtures for some reason
|
|
@@ -348,21 +384,24 @@ await switchScenario(page, scenarioId, {
|
|
|
348
384
|
### ❌ Don't: Switch scenarios after navigation
|
|
349
385
|
|
|
350
386
|
```typescript
|
|
351
|
-
import { test } from
|
|
387
|
+
import { test, expect } from "./fixtures"; // Import from YOUR fixtures file
|
|
352
388
|
|
|
353
|
-
test(
|
|
354
|
-
await page.goto(
|
|
355
|
-
await switchScenario(page,
|
|
389
|
+
test("bad example", async ({ page, switchScenario }) => {
|
|
390
|
+
await page.goto("/"); // BAD - Navigating first
|
|
391
|
+
await switchScenario(page, "premium"); // Headers set too late!
|
|
356
392
|
});
|
|
357
393
|
```
|
|
358
394
|
|
|
359
395
|
**Why it fails**: Headers set AFTER navigation don't affect the already-loaded page.
|
|
360
396
|
|
|
361
397
|
**Solution**: ✅ Switch scenario BEFORE navigating:
|
|
398
|
+
|
|
362
399
|
```typescript
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
400
|
+
import { test, expect } from "./fixtures";
|
|
401
|
+
|
|
402
|
+
test("good example", async ({ page, switchScenario }) => {
|
|
403
|
+
await switchScenario(page, "premium"); // Set headers first
|
|
404
|
+
await page.goto("/"); // Now requests use test ID header
|
|
366
405
|
});
|
|
367
406
|
```
|
|
368
407
|
|
|
@@ -382,11 +421,12 @@ export default defineConfig({
|
|
|
382
421
|
**Error**: `switchScenario` won't know where to send requests.
|
|
383
422
|
|
|
384
423
|
**Solution**: ✅ Configure in `playwright.config.ts`:
|
|
424
|
+
|
|
385
425
|
```typescript
|
|
386
426
|
export default defineConfig({
|
|
387
427
|
use: {
|
|
388
|
-
baseURL:
|
|
389
|
-
scenaristEndpoint:
|
|
428
|
+
baseURL: "http://localhost:3000",
|
|
429
|
+
scenaristEndpoint: "/api/__scenario__",
|
|
390
430
|
},
|
|
391
431
|
});
|
|
392
432
|
```
|
|
@@ -396,14 +436,14 @@ export default defineConfig({
|
|
|
396
436
|
### ❌ Don't: Use standalone `switchScenario` with manual test IDs
|
|
397
437
|
|
|
398
438
|
```typescript
|
|
399
|
-
import { switchScenario } from
|
|
439
|
+
import { switchScenario } from "@scenarist/playwright-helpers";
|
|
400
440
|
|
|
401
|
-
test(
|
|
441
|
+
test("bad example", async ({ page }) => {
|
|
402
442
|
// BAD - Manual test ID risks conflicts
|
|
403
|
-
await switchScenario(page,
|
|
404
|
-
baseURL:
|
|
405
|
-
endpoint:
|
|
406
|
-
testId:
|
|
443
|
+
await switchScenario(page, "premium", {
|
|
444
|
+
baseURL: "http://localhost:3000",
|
|
445
|
+
endpoint: "/api/__scenario__",
|
|
446
|
+
testId: "my-test", // Same ID across parallel tests = collision!
|
|
407
447
|
});
|
|
408
448
|
});
|
|
409
449
|
```
|
|
@@ -411,11 +451,12 @@ test('bad example', async ({ page }) => {
|
|
|
411
451
|
**Why it fails**: Multiple tests with the same ID will interfere with each other in parallel execution.
|
|
412
452
|
|
|
413
453
|
**Solution**: ✅ Use the fixture API (auto-generates unique IDs):
|
|
454
|
+
|
|
414
455
|
```typescript
|
|
415
|
-
import { test } from
|
|
456
|
+
import { test, expect } from "./fixtures"; // Import from YOUR fixtures file
|
|
416
457
|
|
|
417
|
-
test(
|
|
418
|
-
await switchScenario(page,
|
|
458
|
+
test("good example", async ({ page, switchScenario }) => {
|
|
459
|
+
await switchScenario(page, "premium");
|
|
419
460
|
// Generates unique ID automatically: test-abc123-{uuid}
|
|
420
461
|
});
|
|
421
462
|
```
|
|
@@ -427,6 +468,7 @@ test('good example', async ({ page, switchScenario }) => {
|
|
|
427
468
|
**Answer**: We use a **two-layer approach** that tests different concerns:
|
|
428
469
|
|
|
429
470
|
**Why this matters**:
|
|
471
|
+
|
|
430
472
|
- ⚡ Fast feedback (1.7s vs 2-3s for full E2E)
|
|
431
473
|
- 🎯 Pinpoint issues in Playwright integration vs framework integration
|
|
432
474
|
- 📋 Comprehensive edge case coverage (13 test scenarios)
|
|
@@ -439,6 +481,7 @@ This package uses **real Playwright integration** in tests, which is an **except
|
|
|
439
481
|
**⚠️ This is NOT the standard approach.** Most adapters (Express, Next.js, etc.) should mock external dependencies for fast, focused tests.
|
|
440
482
|
|
|
441
483
|
**Why this package qualifies for exception** (per [ADR-0006](../../docs/adrs/0006-thin-adapters-real-integration-tests.md)):
|
|
484
|
+
|
|
442
485
|
- ✅ Extremely thin (~40 lines)
|
|
443
486
|
- ✅ Direct API wrappers only (no transformation)
|
|
444
487
|
- ✅ Stable API (Playwright)
|
|
@@ -456,11 +499,13 @@ This package uses a **two-layer testing approach**:
|
|
|
456
499
|
These tests validate the helper works correctly with **real Playwright** using MSW Node server. They prove the helper integrates with Playwright's Page API correctly.
|
|
457
500
|
|
|
458
501
|
**Testing Stack**:
|
|
502
|
+
|
|
459
503
|
- ✅ **Real Playwright** - Uses actual `@playwright/test` with real Page objects
|
|
460
504
|
- ✅ **MSW Node Server** - Real HTTP server responding to requests
|
|
461
505
|
- ✅ **No framework dependencies** - Tests helper + Playwright only
|
|
462
506
|
|
|
463
507
|
**What We Test**:
|
|
508
|
+
|
|
464
509
|
- ✅ Scenario switching succeeds with correct inputs
|
|
465
510
|
- ✅ Test ID generation format and uniqueness
|
|
466
511
|
- ✅ Endpoint URL construction (baseURL + endpoint)
|
|
@@ -473,17 +518,21 @@ These tests validate the helper works correctly with **real Playwright** using M
|
|
|
473
518
|
**Value**: Tests helper's integration with Playwright in isolation. No Next.js, no MSW client, no complex stack - just helper + Playwright + HTTP server.
|
|
474
519
|
|
|
475
520
|
**Example Test**:
|
|
521
|
+
|
|
476
522
|
```typescript
|
|
477
|
-
test(
|
|
523
|
+
test("should throw error when scenario switch fails with 404", async ({
|
|
524
|
+
page,
|
|
525
|
+
}) => {
|
|
478
526
|
await expect(
|
|
479
|
-
switchScenario(page,
|
|
480
|
-
baseURL:
|
|
481
|
-
})
|
|
527
|
+
switchScenario(page, "error-404", {
|
|
528
|
+
baseURL: "http://localhost:9876",
|
|
529
|
+
}),
|
|
482
530
|
).rejects.toThrow(/Failed to switch scenario: 404/);
|
|
483
531
|
});
|
|
484
532
|
```
|
|
485
533
|
|
|
486
534
|
**Run Package Tests**:
|
|
535
|
+
|
|
487
536
|
```bash
|
|
488
537
|
cd packages/playwright-helpers
|
|
489
538
|
pnpm test # Run all tests
|
|
@@ -497,6 +546,7 @@ pnpm test:watch # Interactive UI mode
|
|
|
497
546
|
These tests validate the **complete integration** of helpers with real frameworks, real browsers, and real scenario endpoints.
|
|
498
547
|
|
|
499
548
|
**What Integration Tests Cover**:
|
|
549
|
+
|
|
500
550
|
- ✅ Helper works with real Playwright browsers (Chromium/Firefox/WebKit)
|
|
501
551
|
- ✅ Helper works with real Next.js API routes
|
|
502
552
|
- ✅ Helper works with real Scenarist scenario switching
|
|
@@ -505,18 +555,19 @@ These tests validate the **complete integration** of helpers with real framework
|
|
|
505
555
|
|
|
506
556
|
**Why Both Layers Matter**:
|
|
507
557
|
|
|
508
|
-
| Concern
|
|
509
|
-
|
|
510
|
-
| **Speed**
|
|
511
|
-
| **Scope**
|
|
512
|
-
| **Debugging**
|
|
513
|
-
| **Coverage**
|
|
514
|
-
| **Dependencies**
|
|
515
|
-
| **What it proves** | Helper works with Playwright API
|
|
558
|
+
| Concern | Package Tests ✅ | Integration Tests ✅ |
|
|
559
|
+
| ------------------ | ----------------------------------------- | -------------------------------------- |
|
|
560
|
+
| **Speed** | ⚡ Fast (1.7s) | 🐌 Slower (2-3s) |
|
|
561
|
+
| **Scope** | Helper + Playwright only | Full stack (Next.js + MSW + Scenarist) |
|
|
562
|
+
| **Debugging** | 🎯 Pinpoint Playwright integration issues | 🔍 Find framework integration issues |
|
|
563
|
+
| **Coverage** | Edge cases + error handling | Happy paths + real scenarios |
|
|
564
|
+
| **Dependencies** | Minimal (Playwright, MSW) | Many (Next.js, MSW client, Scenarist) |
|
|
565
|
+
| **What it proves** | Helper works with Playwright API | Helper works with real frameworks |
|
|
516
566
|
|
|
517
567
|
**Takeaway**: Package tests prove Playwright integration. Integration tests prove framework integration.
|
|
518
568
|
|
|
519
569
|
**Run Integration Tests**:
|
|
570
|
+
|
|
520
571
|
```bash
|
|
521
572
|
# From repository root
|
|
522
573
|
pnpm --filter=@scenarist/nextjs-pages-router-example test:e2e
|
|
@@ -529,15 +580,18 @@ pnpm --filter=@scenarist/nextjs-pages-router-example test:e2e
|
|
|
529
580
|
**Answer**: This follows strict TDD with **deliberate refactoring**:
|
|
530
581
|
|
|
531
582
|
**Phase 1a (RED)**: E2E test written first
|
|
583
|
+
|
|
532
584
|
- ❌ Test fails: "switchScenario is not a function"
|
|
533
585
|
- **This is the failing test that drove development**
|
|
534
586
|
|
|
535
587
|
**Phase 1b (GREEN)**: Minimum implementation
|
|
588
|
+
|
|
536
589
|
- ✅ Helper implementation created
|
|
537
590
|
- ✅ E2E test passes
|
|
538
591
|
- **Working code, ready to commit**
|
|
539
592
|
|
|
540
593
|
**Phase 1c (REFACTOR)**: Edge case coverage added
|
|
594
|
+
|
|
541
595
|
- 🔧 17 behavior tests added (error handling, edge cases, defaults)
|
|
542
596
|
- ✅ All tests green, 100% coverage achieved
|
|
543
597
|
- **Refactoring step per RED-GREEN-REFACTOR cycle**
|
|
@@ -550,14 +604,15 @@ This is textbook TDD: **RED** (E2E fails) → **GREEN** (implementation) → **R
|
|
|
550
604
|
|
|
551
605
|
Our testing guidelines (from `docs/testing-guidelines.md`) require:
|
|
552
606
|
|
|
553
|
-
| Guideline
|
|
554
|
-
|
|
555
|
-
| **"Test through public API exclusively"**
|
|
556
|
-
| **"No 1:1 mapping between test/implementation files"** | ✅ Single test file, single public function, multiple behaviors
|
|
557
|
-
| **"Tests must document expected business behaviour"**
|
|
558
|
-
| **"Behavior-driven - treat as black box"**
|
|
607
|
+
| Guideline | How Package Tests Comply |
|
|
608
|
+
| ------------------------------------------------------ | --------------------------------------------------------------------- |
|
|
609
|
+
| **"Test through public API exclusively"** | ✅ Tests call `switchScenario()` only - internals invisible |
|
|
610
|
+
| **"No 1:1 mapping between test/implementation files"** | ✅ Single test file, single public function, multiple behaviors |
|
|
611
|
+
| **"Tests must document expected business behaviour"** | ✅ Each test describes observable behavior (error, headers, defaults) |
|
|
612
|
+
| **"Behavior-driven - treat as black box"** | ✅ Tests verify inputs → outputs, implementation is opaque |
|
|
559
613
|
|
|
560
614
|
**Key Distinction**:
|
|
615
|
+
|
|
561
616
|
- ❌ **Unit testing** = Testing internal functions, mocking own code, coupling to implementation
|
|
562
617
|
- ✅ **Behavior testing** = Testing public API, mocking external dependencies, verifying outcomes
|
|
563
618
|
|
package/dist/fixtures.d.ts
CHANGED
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
* For complete documentation, see the README at:
|
|
93
93
|
* https://github.com/citypaul/scenarist/tree/main/packages/playwright-helpers
|
|
94
94
|
*/
|
|
95
|
-
import { type Page } from
|
|
96
|
-
import { type SwitchScenarioOptions } from
|
|
97
|
-
import type { ScenaristScenarios, ScenarioIds } from
|
|
95
|
+
import { type Page } from "@playwright/test";
|
|
96
|
+
import { type SwitchScenarioOptions } from "./switch-scenario.js";
|
|
97
|
+
import type { ScenaristScenarios, ScenarioIds } from "@scenarist/core";
|
|
98
98
|
/**
|
|
99
99
|
* Configuration options for Scenarist.
|
|
100
100
|
*
|
|
@@ -160,7 +160,7 @@ export type ScenaristFixtures<S extends string = string> = {
|
|
|
160
160
|
* });
|
|
161
161
|
* ```
|
|
162
162
|
*/
|
|
163
|
-
switchScenario: (page: Page, scenarioId: S, options?: Partial<Pick<SwitchScenarioOptions,
|
|
163
|
+
switchScenario: (page: Page, scenarioId: S, options?: Partial<Pick<SwitchScenarioOptions, "endpoint" | "baseURL">>) => Promise<string>;
|
|
164
164
|
};
|
|
165
165
|
/**
|
|
166
166
|
* Create a type-safe Playwright test object with Scenarist fixtures.
|
package/dist/fixtures.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixtures.d.ts","sourceRoot":"","sources":["../src/fixtures.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6FG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"fixtures.d.ts","sourceRoot":"","sources":["../src/fixtures.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6FG;AAEH,OAAO,EAGL,KAAK,IAAI,EACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAEL,KAAK,qBAAqB,EAC3B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEvE;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;;;;;;;;;;OAaG;IACH,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IACzD;;;;;;;OAOG;IACH,eAAe,EAAE,MAAM,CAAC;IAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,cAAc,EAAE,CACd,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,CAAC,EACb,OAAO,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,qBAAqB,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC,KACnE,OAAO,CAAC,MAAM,CAAC,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,kBAAkB,EAAE,SAAS,EAAE,CAAC,sSA4CvE;AAED,eAAO,MAAM,MAAM,uCAAa,CAAC"}
|
package/dist/fixtures.js
CHANGED
|
@@ -92,8 +92,8 @@
|
|
|
92
92
|
* For complete documentation, see the README at:
|
|
93
93
|
* https://github.com/citypaul/scenarist/tree/main/packages/playwright-helpers
|
|
94
94
|
*/
|
|
95
|
-
import { test as base, expect as baseExpect } from
|
|
96
|
-
import { switchScenario as baseSwitchScenario } from
|
|
95
|
+
import { test as base, expect as baseExpect, } from "@playwright/test";
|
|
96
|
+
import { switchScenario as baseSwitchScenario, } from "./switch-scenario.js";
|
|
97
97
|
/**
|
|
98
98
|
* Create a type-safe Playwright test object with Scenarist fixtures.
|
|
99
99
|
*
|
|
@@ -131,7 +131,7 @@ export function withScenarios(scenarios) {
|
|
|
131
131
|
void scenarios;
|
|
132
132
|
return base.extend({
|
|
133
133
|
// Configuration option with default value
|
|
134
|
-
scenaristEndpoint: [
|
|
134
|
+
scenaristEndpoint: ["/api/__scenario__", { option: true }],
|
|
135
135
|
// Generate a guaranteed unique test ID for each test
|
|
136
136
|
scenaristTestId: async ({}, use, testInfo) => {
|
|
137
137
|
// Use test info + UUID for guaranteed uniqueness
|
|
@@ -143,7 +143,7 @@ export function withScenarios(scenarios) {
|
|
|
143
143
|
const switchFn = async (page, scenarioId, options) => {
|
|
144
144
|
// Read from config, allow per-test override, fallback to defaults
|
|
145
145
|
const finalEndpoint = options?.endpoint ?? scenaristEndpoint;
|
|
146
|
-
const finalBaseURL = options?.baseURL ?? baseURL ??
|
|
146
|
+
const finalBaseURL = options?.baseURL ?? baseURL ?? "http://localhost:3000";
|
|
147
147
|
// Call the base switchScenario with all config and return test ID
|
|
148
148
|
return await baseSwitchScenario(page, scenarioId, {
|
|
149
149
|
endpoint: finalEndpoint,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { switchScenario, type SwitchScenarioOptions } from
|
|
2
|
-
export { withScenarios, expect, type ScenaristOptions, type ScenaristFixtures } from
|
|
3
|
-
export { SCENARIST_TEST_ID_HEADER } from
|
|
1
|
+
export { switchScenario, type SwitchScenarioOptions, } from "./switch-scenario.js";
|
|
2
|
+
export { withScenarios, expect, type ScenaristOptions, type ScenaristFixtures, } from "./fixtures.js";
|
|
3
|
+
export { SCENARIST_TEST_ID_HEADER } from "@scenarist/core";
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,qBAAqB,GAC3B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,aAAa,EACb,MAAM,EACN,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACvB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { switchScenario } from
|
|
2
|
-
export { withScenarios, expect } from
|
|
1
|
+
export { switchScenario, } from "./switch-scenario.js";
|
|
2
|
+
export { withScenarios, expect, } from "./fixtures.js";
|
|
3
3
|
// Re-export header constant for user convenience
|
|
4
|
-
export { SCENARIST_TEST_ID_HEADER } from
|
|
4
|
+
export { SCENARIST_TEST_ID_HEADER } from "@scenarist/core";
|
package/dist/switch-scenario.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
|
* Establish route interception and header injection for test ID propagation.
|
|
4
4
|
*
|
|
@@ -11,9 +11,9 @@ import { SCENARIST_TEST_ID_HEADER } from '@scenarist/core';
|
|
|
11
11
|
*/
|
|
12
12
|
const establishTestIdInterception = async (page, testId) => {
|
|
13
13
|
// Clear any existing handlers to prevent accumulation
|
|
14
|
-
await page.unroute(
|
|
14
|
+
await page.unroute("**/*");
|
|
15
15
|
// Intercept all routes to inject test ID header
|
|
16
|
-
await page.route(
|
|
16
|
+
await page.route("**/*", async (route) => {
|
|
17
17
|
const headers = route.request().headers();
|
|
18
18
|
headers[SCENARIST_TEST_ID_HEADER] = testId;
|
|
19
19
|
await route.continue({ headers });
|
|
@@ -54,7 +54,7 @@ const establishTestIdInterception = async (page, testId) => {
|
|
|
54
54
|
* ```
|
|
55
55
|
*/
|
|
56
56
|
export const switchScenario = async (page, scenarioId, options) => {
|
|
57
|
-
const { baseURL, endpoint =
|
|
57
|
+
const { baseURL, endpoint = "/__scenario__", testId: providedTestId, } = options;
|
|
58
58
|
// Use provided test ID (from fixture) or generate unique one
|
|
59
59
|
// When using fixtures, test ID is guaranteed unique by Playwright's test context
|
|
60
60
|
// Date.now() can collide when tests run in parallel within the same millisecond
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scenarist/playwright-helpers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Playwright test helpers for Scenarist scenario management",
|
|
5
5
|
"author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,10 +29,12 @@
|
|
|
29
29
|
},
|
|
30
30
|
"type": "module",
|
|
31
31
|
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
32
34
|
"exports": {
|
|
33
35
|
".": {
|
|
34
36
|
"types": "./dist/index.d.ts",
|
|
35
|
-
"
|
|
37
|
+
"default": "./dist/index.js"
|
|
36
38
|
}
|
|
37
39
|
},
|
|
38
40
|
"files": [
|