@scenarist/playwright-helpers 0.1.9 → 0.1.11
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 +1 -173
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ Playwright test helpers for [Scenarist](https://scenarist.io) scenario managemen
|
|
|
13
13
|
| -------------------------- | -------------------------------------------------------------------------------------------------- |
|
|
14
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
|
+
| **Tool Comparison** | [scenarist.io/comparison](https://scenarist.io/comparison) |
|
|
16
17
|
| **Parallel Testing** | [scenarist.io/testing/parallel-testing](https://scenarist.io/testing/parallel-testing) |
|
|
17
18
|
| **Testing Philosophy** | [scenarist.io/concepts/philosophy](https://scenarist.io/concepts/philosophy) |
|
|
18
19
|
|
|
@@ -489,179 +490,6 @@ test("good example", async ({ page, switchScenario }) => {
|
|
|
489
490
|
});
|
|
490
491
|
```
|
|
491
492
|
|
|
492
|
-
## Testing Philosophy
|
|
493
|
-
|
|
494
|
-
**You might be thinking**: "Why test the helpers in their own package when they're already tested in Next.js?"
|
|
495
|
-
|
|
496
|
-
**Answer**: We use a **two-layer approach** that tests different concerns:
|
|
497
|
-
|
|
498
|
-
**Why this matters**:
|
|
499
|
-
|
|
500
|
-
- ⚡ Fast feedback (1.7s vs 2-3s for full E2E)
|
|
501
|
-
- 🎯 Pinpoint issues in Playwright integration vs framework integration
|
|
502
|
-
- 📋 Comprehensive edge case coverage (13 test scenarios)
|
|
503
|
-
- 🔒 Tests prove helper works with real Playwright API
|
|
504
|
-
|
|
505
|
-
### Testing Strategy: Real Playwright Integration (Exception to Layer 2 Rule)
|
|
506
|
-
|
|
507
|
-
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).
|
|
508
|
-
|
|
509
|
-
**⚠️ This is NOT the standard approach.** Most adapters (Express, Next.js, etc.) should mock external dependencies for fast, focused tests.
|
|
510
|
-
|
|
511
|
-
**Why this package qualifies for exception** (per [ADR-0006](../../docs/adrs/0006-thin-adapters-real-integration-tests.md)):
|
|
512
|
-
|
|
513
|
-
- ✅ Extremely thin (~40 lines)
|
|
514
|
-
- ✅ Direct API wrappers only (no transformation)
|
|
515
|
-
- ✅ Stable API (Playwright)
|
|
516
|
-
- ✅ Fast tests (1.7s)
|
|
517
|
-
- ✅ Real integration provides significantly higher confidence
|
|
518
|
-
|
|
519
|
-
See [ADR-0006](../../docs/adrs/0006-thin-adapters-real-integration-tests.md) for full decision criteria and when to use real dependencies vs. mocks.
|
|
520
|
-
|
|
521
|
-
This package uses a **two-layer testing approach**:
|
|
522
|
-
|
|
523
|
-
### Layer 1: Playwright Integration Tests (This Package)
|
|
524
|
-
|
|
525
|
-
**Location**: `packages/playwright-helpers/tests/`
|
|
526
|
-
|
|
527
|
-
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.
|
|
528
|
-
|
|
529
|
-
**Testing Stack**:
|
|
530
|
-
|
|
531
|
-
- ✅ **Real Playwright** - Uses actual `@playwright/test` with real Page objects
|
|
532
|
-
- ✅ **MSW Node Server** - Real HTTP server responding to requests
|
|
533
|
-
- ✅ **No framework dependencies** - Tests helper + Playwright only
|
|
534
|
-
|
|
535
|
-
**What We Test**:
|
|
536
|
-
|
|
537
|
-
- ✅ Scenario switching succeeds with correct inputs
|
|
538
|
-
- ✅ Test ID generation format and uniqueness
|
|
539
|
-
- ✅ Endpoint URL construction (baseURL + endpoint)
|
|
540
|
-
- ✅ Request body structure (scenario, variant)
|
|
541
|
-
- ✅ Custom header support (testIdHeader)
|
|
542
|
-
- ✅ Error handling (404, 400, 500 responses)
|
|
543
|
-
- ✅ Multiple concurrent scenarios work independently
|
|
544
|
-
- ✅ Default values for optional parameters
|
|
545
|
-
|
|
546
|
-
**Value**: Tests helper's integration with Playwright in isolation. No Next.js, no MSW client, no complex stack - just helper + Playwright + HTTP server.
|
|
547
|
-
|
|
548
|
-
**Example Test**:
|
|
549
|
-
|
|
550
|
-
```typescript
|
|
551
|
-
test("should throw error when scenario switch fails with 404", async ({
|
|
552
|
-
page,
|
|
553
|
-
}) => {
|
|
554
|
-
await expect(
|
|
555
|
-
switchScenario(page, "error-404", {
|
|
556
|
-
baseURL: "http://localhost:9876",
|
|
557
|
-
}),
|
|
558
|
-
).rejects.toThrow(/Failed to switch scenario: 404/);
|
|
559
|
-
});
|
|
560
|
-
```
|
|
561
|
-
|
|
562
|
-
**Run Package Tests**:
|
|
563
|
-
|
|
564
|
-
```bash
|
|
565
|
-
cd packages/playwright-helpers
|
|
566
|
-
pnpm test # Run all tests
|
|
567
|
-
pnpm test:watch # Interactive UI mode
|
|
568
|
-
```
|
|
569
|
-
|
|
570
|
-
### Layer 2: Integration Tests (Next.js App)
|
|
571
|
-
|
|
572
|
-
**Location**: `apps/nextjs-pages-router-example/tests/playwright/scenario-switching.spec.ts`
|
|
573
|
-
|
|
574
|
-
These tests validate the **complete integration** of helpers with real frameworks, real browsers, and real scenario endpoints.
|
|
575
|
-
|
|
576
|
-
**What Integration Tests Cover**:
|
|
577
|
-
|
|
578
|
-
- ✅ Helper works with real Playwright browsers (Chromium/Firefox/WebKit)
|
|
579
|
-
- ✅ Helper works with real Next.js API routes
|
|
580
|
-
- ✅ Helper works with real Scenarist scenario switching
|
|
581
|
-
- ✅ Full user journey (switch → navigate → verify)
|
|
582
|
-
- ✅ Next.js-specific behavior (Pages Router conventions)
|
|
583
|
-
|
|
584
|
-
**Why Both Layers Matter**:
|
|
585
|
-
|
|
586
|
-
| Concern | Package Tests ✅ | Integration Tests ✅ |
|
|
587
|
-
| ------------------ | ----------------------------------------- | -------------------------------------- |
|
|
588
|
-
| **Speed** | ⚡ Fast (1.7s) | 🐌 Slower (2-3s) |
|
|
589
|
-
| **Scope** | Helper + Playwright only | Full stack (Next.js + MSW + Scenarist) |
|
|
590
|
-
| **Debugging** | 🎯 Pinpoint Playwright integration issues | 🔍 Find framework integration issues |
|
|
591
|
-
| **Coverage** | Edge cases + error handling | Happy paths + real scenarios |
|
|
592
|
-
| **Dependencies** | Minimal (Playwright, MSW) | Many (Next.js, MSW client, Scenarist) |
|
|
593
|
-
| **What it proves** | Helper works with Playwright API | Helper works with real frameworks |
|
|
594
|
-
|
|
595
|
-
**Takeaway**: Package tests prove Playwright integration. Integration tests prove framework integration.
|
|
596
|
-
|
|
597
|
-
**Run Integration Tests**:
|
|
598
|
-
|
|
599
|
-
```bash
|
|
600
|
-
# From repository root
|
|
601
|
-
pnpm --filter=@scenarist/nextjs-pages-router-example test:e2e
|
|
602
|
-
```
|
|
603
|
-
|
|
604
|
-
### TDD Compliance
|
|
605
|
-
|
|
606
|
-
**Critical Question**: "If these aren't unit tests, why were they written AFTER the E2E tests?"
|
|
607
|
-
|
|
608
|
-
**Answer**: This follows strict TDD with **deliberate refactoring**:
|
|
609
|
-
|
|
610
|
-
**Phase 1a (RED)**: E2E test written first
|
|
611
|
-
|
|
612
|
-
- ❌ Test fails: "switchScenario is not a function"
|
|
613
|
-
- **This is the failing test that drove development**
|
|
614
|
-
|
|
615
|
-
**Phase 1b (GREEN)**: Minimum implementation
|
|
616
|
-
|
|
617
|
-
- ✅ Helper implementation created
|
|
618
|
-
- ✅ E2E test passes
|
|
619
|
-
- **Working code, ready to commit**
|
|
620
|
-
|
|
621
|
-
**Phase 1c (REFACTOR)**: Edge case coverage added
|
|
622
|
-
|
|
623
|
-
- 🔧 17 behavior tests added (error handling, edge cases, defaults)
|
|
624
|
-
- ✅ All tests green, 100% coverage achieved
|
|
625
|
-
- **Refactoring step per RED-GREEN-REFACTOR cycle**
|
|
626
|
-
|
|
627
|
-
**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.
|
|
628
|
-
|
|
629
|
-
This is textbook TDD: **RED** (E2E fails) → **GREEN** (implementation) → **REFACTOR** (add package tests for completeness).
|
|
630
|
-
|
|
631
|
-
### How This Aligns With Testing Guidelines
|
|
632
|
-
|
|
633
|
-
Our testing guidelines (from `docs/testing-guidelines.md`) require:
|
|
634
|
-
|
|
635
|
-
| Guideline | How Package Tests Comply |
|
|
636
|
-
| ------------------------------------------------------ | --------------------------------------------------------------------- |
|
|
637
|
-
| **"Test through public API exclusively"** | ✅ Tests call `switchScenario()` only - internals invisible |
|
|
638
|
-
| **"No 1:1 mapping between test/implementation files"** | ✅ Single test file, single public function, multiple behaviors |
|
|
639
|
-
| **"Tests must document expected business behaviour"** | ✅ Each test describes observable behavior (error, headers, defaults) |
|
|
640
|
-
| **"Behavior-driven - treat as black box"** | ✅ Tests verify inputs → outputs, implementation is opaque |
|
|
641
|
-
|
|
642
|
-
**Key Distinction**:
|
|
643
|
-
|
|
644
|
-
- ❌ **Unit testing** = Testing internal functions, mocking own code, coupling to implementation
|
|
645
|
-
- ✅ **Behavior testing** = Testing public API, mocking external dependencies, verifying outcomes
|
|
646
|
-
|
|
647
|
-
We mock Playwright's `Page` (external dependency we don't own), not our own code.
|
|
648
|
-
|
|
649
|
-
## Development
|
|
650
|
-
|
|
651
|
-
```bash
|
|
652
|
-
# Build the package
|
|
653
|
-
pnpm build
|
|
654
|
-
|
|
655
|
-
# Type check
|
|
656
|
-
pnpm typecheck
|
|
657
|
-
|
|
658
|
-
# Lint
|
|
659
|
-
pnpm lint
|
|
660
|
-
|
|
661
|
-
# Clean build artifacts
|
|
662
|
-
pnpm clean
|
|
663
|
-
```
|
|
664
|
-
|
|
665
493
|
## Why This Package Exists
|
|
666
494
|
|
|
667
495
|
Before this helper, switching scenarios in Playwright tests required significant boilerplate:
|
package/package.json
CHANGED