@scenarist/playwright-helpers 0.1.8 → 0.1.10

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 CHANGED
@@ -233,12 +233,14 @@ test("premium user scenario", async ({ page }) => {
233
233
  ```typescript
234
234
  type SwitchScenarioOptions = {
235
235
  readonly baseURL: string; // Base URL of your application
236
- readonly endpoint?: string; // Scenario endpoint path (default: '/__scenario__')
236
+ readonly endpoint?: string; // Scenario endpoint path or absolute URL (default: '/__scenario__')
237
237
  readonly testIdHeader?: string; // Test ID header name (default: 'x-scenarist-test-id')
238
238
  readonly variant?: string; // Optional scenario variant
239
239
  };
240
240
  ```
241
241
 
242
+ > **Tip:** Use an absolute URL for `endpoint` when your API runs on a different host/port than the frontend. See [Cross-Origin API Servers](#cross-origin-api-servers).
243
+
242
244
  #### What it does
243
245
 
244
246
  The `switchScenario` helper:
@@ -330,7 +332,33 @@ export default defineConfig({
330
332
 
331
333
  **Available options:**
332
334
 
333
- - `scenaristEndpoint?: string` - The endpoint path for scenario switching (default: `'/api/__scenario__'`)
335
+ - `scenaristEndpoint?: string` - The endpoint path or absolute URL for scenario switching (default: `'/api/__scenario__'`)
336
+
337
+ ### Cross-Origin API Servers
338
+
339
+ When your API server runs on a different host or port than your frontend, use an **absolute URL** for `scenaristEndpoint`:
340
+
341
+ ```typescript
342
+ // Frontend: http://localhost:3000
343
+ // API Server: http://localhost:9090
344
+ export default defineConfig<ScenaristOptions>({
345
+ use: {
346
+ baseURL: "http://localhost:3000", // Frontend URL (for Playwright navigation)
347
+ scenaristEndpoint: "http://localhost:9090/__scenario__", // Absolute URL to API
348
+ },
349
+ });
350
+ ```
351
+
352
+ **How it works:**
353
+
354
+ - **Relative paths** (e.g., `/api/__scenario__`) are prepended with `baseURL`
355
+ - **Absolute URLs** (starting with `http://` or `https://`) are used directly, ignoring `baseURL`
356
+
357
+ This is useful when:
358
+
359
+ - Your API and frontend are separate services on different ports
360
+ - You're testing against a staging/production API endpoint
361
+ - Your test infrastructure uses a separate mock server
334
362
 
335
363
  #### `switchScenario` (Fixture)
336
364
 
@@ -461,179 +489,6 @@ test("good example", async ({ page, switchScenario }) => {
461
489
  });
462
490
  ```
463
491
 
464
- ## Testing Philosophy
465
-
466
- **You might be thinking**: "Why test the helpers in their own package when they're already tested in Next.js?"
467
-
468
- **Answer**: We use a **two-layer approach** that tests different concerns:
469
-
470
- **Why this matters**:
471
-
472
- - ⚡ Fast feedback (1.7s vs 2-3s for full E2E)
473
- - 🎯 Pinpoint issues in Playwright integration vs framework integration
474
- - 📋 Comprehensive edge case coverage (13 test scenarios)
475
- - 🔒 Tests prove helper works with real Playwright API
476
-
477
- ### Testing Strategy: Real Playwright Integration (Exception to Layer 2 Rule)
478
-
479
- This package uses **real Playwright integration** in tests, which is an **exception** to the general adapter testing rule (ADR-0003 Layer 2 prescribes mocking external dependencies).
480
-
481
- **⚠️ This is NOT the standard approach.** Most adapters (Express, Next.js, etc.) should mock external dependencies for fast, focused tests.
482
-
483
- **Why this package qualifies for exception** (per [ADR-0006](../../docs/adrs/0006-thin-adapters-real-integration-tests.md)):
484
-
485
- - ✅ Extremely thin (~40 lines)
486
- - ✅ Direct API wrappers only (no transformation)
487
- - ✅ Stable API (Playwright)
488
- - ✅ Fast tests (1.7s)
489
- - ✅ Real integration provides significantly higher confidence
490
-
491
- See [ADR-0006](../../docs/adrs/0006-thin-adapters-real-integration-tests.md) for full decision criteria and when to use real dependencies vs. mocks.
492
-
493
- This package uses a **two-layer testing approach**:
494
-
495
- ### Layer 1: Playwright Integration Tests (This Package)
496
-
497
- **Location**: `packages/playwright-helpers/tests/`
498
-
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.
500
-
501
- **Testing Stack**:
502
-
503
- - ✅ **Real Playwright** - Uses actual `@playwright/test` with real Page objects
504
- - ✅ **MSW Node Server** - Real HTTP server responding to requests
505
- - ✅ **No framework dependencies** - Tests helper + Playwright only
506
-
507
- **What We Test**:
508
-
509
- - ✅ Scenario switching succeeds with correct inputs
510
- - ✅ Test ID generation format and uniqueness
511
- - ✅ Endpoint URL construction (baseURL + endpoint)
512
- - ✅ Request body structure (scenario, variant)
513
- - ✅ Custom header support (testIdHeader)
514
- - ✅ Error handling (404, 400, 500 responses)
515
- - ✅ Multiple concurrent scenarios work independently
516
- - ✅ Default values for optional parameters
517
-
518
- **Value**: Tests helper's integration with Playwright in isolation. No Next.js, no MSW client, no complex stack - just helper + Playwright + HTTP server.
519
-
520
- **Example Test**:
521
-
522
- ```typescript
523
- test("should throw error when scenario switch fails with 404", async ({
524
- page,
525
- }) => {
526
- await expect(
527
- switchScenario(page, "error-404", {
528
- baseURL: "http://localhost:9876",
529
- }),
530
- ).rejects.toThrow(/Failed to switch scenario: 404/);
531
- });
532
- ```
533
-
534
- **Run Package Tests**:
535
-
536
- ```bash
537
- cd packages/playwright-helpers
538
- pnpm test # Run all tests
539
- pnpm test:watch # Interactive UI mode
540
- ```
541
-
542
- ### Layer 2: Integration Tests (Next.js App)
543
-
544
- **Location**: `apps/nextjs-pages-router-example/tests/playwright/scenario-switching.spec.ts`
545
-
546
- These tests validate the **complete integration** of helpers with real frameworks, real browsers, and real scenario endpoints.
547
-
548
- **What Integration Tests Cover**:
549
-
550
- - ✅ Helper works with real Playwright browsers (Chromium/Firefox/WebKit)
551
- - ✅ Helper works with real Next.js API routes
552
- - ✅ Helper works with real Scenarist scenario switching
553
- - ✅ Full user journey (switch → navigate → verify)
554
- - ✅ Next.js-specific behavior (Pages Router conventions)
555
-
556
- **Why Both Layers Matter**:
557
-
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 |
566
-
567
- **Takeaway**: Package tests prove Playwright integration. Integration tests prove framework integration.
568
-
569
- **Run Integration Tests**:
570
-
571
- ```bash
572
- # From repository root
573
- pnpm --filter=@scenarist/nextjs-pages-router-example test:e2e
574
- ```
575
-
576
- ### TDD Compliance
577
-
578
- **Critical Question**: "If these aren't unit tests, why were they written AFTER the E2E tests?"
579
-
580
- **Answer**: This follows strict TDD with **deliberate refactoring**:
581
-
582
- **Phase 1a (RED)**: E2E test written first
583
-
584
- - ❌ Test fails: "switchScenario is not a function"
585
- - **This is the failing test that drove development**
586
-
587
- **Phase 1b (GREEN)**: Minimum implementation
588
-
589
- - ✅ Helper implementation created
590
- - ✅ E2E test passes
591
- - **Working code, ready to commit**
592
-
593
- **Phase 1c (REFACTOR)**: Edge case coverage added
594
-
595
- - 🔧 17 behavior tests added (error handling, edge cases, defaults)
596
- - ✅ All tests green, 100% coverage achieved
597
- - **Refactoring step per RED-GREEN-REFACTOR cycle**
598
-
599
- **Key Insight**: Package tests are NOT "test-first" - they're REFACTORING tests added after green to ensure edge case coverage. The E2E test was the failing test that drove implementation.
600
-
601
- This is textbook TDD: **RED** (E2E fails) → **GREEN** (implementation) → **REFACTOR** (add package tests for completeness).
602
-
603
- ### How This Aligns With Testing Guidelines
604
-
605
- Our testing guidelines (from `docs/testing-guidelines.md`) require:
606
-
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 |
613
-
614
- **Key Distinction**:
615
-
616
- - ❌ **Unit testing** = Testing internal functions, mocking own code, coupling to implementation
617
- - ✅ **Behavior testing** = Testing public API, mocking external dependencies, verifying outcomes
618
-
619
- We mock Playwright's `Page` (external dependency we don't own), not our own code.
620
-
621
- ## Development
622
-
623
- ```bash
624
- # Build the package
625
- pnpm build
626
-
627
- # Type check
628
- pnpm typecheck
629
-
630
- # Lint
631
- pnpm lint
632
-
633
- # Clean build artifacts
634
- pnpm clean
635
- ```
636
-
637
492
  ## Why This Package Exists
638
493
 
639
494
  Before this helper, switching scenarios in Playwright tests required significant boilerplate:
@@ -1 +1 @@
1
- {"version":3,"file":"switch-scenario.d.ts","sourceRoot":"","sources":["../src/switch-scenario.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAE3B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AA8BF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,cAAc,GACzB,MAAM,IAAI,EACV,YAAY,MAAM,EAClB,SAAS,qBAAqB,KAC7B,OAAO,CAAC,MAAM,CAmChB,CAAC"}
1
+ {"version":3,"file":"switch-scenario.d.ts","sourceRoot":"","sources":["../src/switch-scenario.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAE3B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AA8BF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,cAAc,GACzB,MAAM,IAAI,EACV,YAAY,MAAM,EAClB,SAAS,qBAAqB,KAC7B,OAAO,CAAC,MAAM,CAsChB,CAAC"}
@@ -63,7 +63,9 @@ export const switchScenario = async (page, scenarioId, options) => {
63
63
  // Client Components may fire API calls immediately on mount/navigation
64
64
  await establishTestIdInterception(page, testId);
65
65
  // Call scenario endpoint
66
- const url = `${baseURL}${endpoint}`;
66
+ // Support absolute URLs for when API is on different host/port than frontend
67
+ const isAbsoluteUrl = endpoint.startsWith("http://") || endpoint.startsWith("https://");
68
+ const url = isAbsoluteUrl ? endpoint : `${baseURL}${endpoint}`;
67
69
  const response = await page.request.post(url, {
68
70
  headers: { [SCENARIST_TEST_ID_HEADER]: testId },
69
71
  data: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenarist/playwright-helpers",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Playwright test helpers for Scenarist scenario management",
5
5
  "author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
6
6
  "license": "MIT",
@@ -50,8 +50,8 @@
50
50
  "@types/node": "^24.10.1",
51
51
  "msw": "^2.12.3",
52
52
  "typescript": "^5.9.3",
53
- "@scenarist/eslint-config": "0.0.0",
54
- "@scenarist/typescript-config": "0.0.0"
53
+ "@scenarist/typescript-config": "0.0.0",
54
+ "@scenarist/eslint-config": "0.0.0"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"