|
| 1 | +## <!-- .cursor/rules/test.playwright.mdc --> |
| 2 | + |
| 3 | +description: "Testing rules (Jest + RTL + Playwright)" |
| 4 | +globs: ["**/*.{ts,tsx}"] |
| 5 | +alwaysApply: true |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# Testing Rules |
| 10 | + |
| 11 | +## General |
| 12 | + |
| 13 | +- File naming: `*.test.ts(x)` for unit/component, `*.spec.ts(x)` for integration/E2E. |
| 14 | +- Co-locate tests with code when small; otherwise keep under `/tests`. |
| 15 | +- Aim for high-value coverage (core logic ~80%+). Skip trivial one-liners. |
| 16 | +- Naming: `describe(Component)` + `it('renders …')` with full-sentence style. |
| 17 | +- **Comments must be in English** for international collaboration and maintainability. |
| 18 | +- **Keep comments minimal and concise** - only add comments when they provide essential context or explain non-obvious behavior. |
| 19 | + |
| 20 | +## Unit/Component (Jest + React Testing Library) |
| 21 | + |
| 22 | +- Test **public behavior** (render, interaction, output). Avoid testing internals. |
| 23 | +- Each critical component should include: |
| 24 | + - Render snapshot (basic mount), |
| 25 | + - Interaction (click, input, keyboard), |
| 26 | + - a11y basics (role/label presence, keyboard navigation), |
| 27 | + - Store selector correctness (mock store when needed). |
| 28 | +- Prefer `screen.getByRole` over `getByTestId` unless no semantic alternative. |
| 29 | +- Use `@testing-library/jest-dom` matchers for DOM assertions. |
| 30 | + |
| 31 | +## Store (Zustand slices) |
| 32 | + |
| 33 | +- Test actions: |
| 34 | + - Initial state correctness, |
| 35 | + - Success/error branches, |
| 36 | + - Immutable updates. |
| 37 | +- Async actions: mock API calls, assert `isLoading/error/data` transitions. |
| 38 | +- Test persistence middleware behavior when applicable. |
| 39 | + |
| 40 | +## E2E (Playwright) |
| 41 | + |
| 42 | +- Cover only critical flows: |
| 43 | + - Happy path: load → interact → result, |
| 44 | + - Core regressions: auth, filters, CRUD happy path. |
| 45 | +- Store screenshots/videos on CI for failed runs. |
| 46 | +- Prefer deterministic locators (role, label, data-testid) over CSS selectors. |
| 47 | +- Use `page.waitForSelector` for dynamic content loading. |
| 48 | + |
| 49 | +## Package-Specific Rules |
| 50 | + |
| 51 | +### View Package |
| 52 | + |
| 53 | +- Test React components with Jest + RTL |
| 54 | +- Test Zustand stores (theme, branch, data, loading, etc.) |
| 55 | +- E2E tests for webview interactions |
| 56 | +- Mock VSCode API calls appropriately |
| 57 | + |
| 58 | +### Analysis Engine Package |
| 59 | + |
| 60 | +- Test core logic with Jest (Node environment) |
| 61 | +- Mock external dependencies (GitHub API, file system) |
| 62 | +- Test parsing and analysis algorithms |
| 63 | + |
| 64 | +### VSCode Extension Package |
| 65 | + |
| 66 | +- Test extension commands and utilities |
| 67 | +- Mock VSCode extension API |
| 68 | +- Test webview communication |
| 69 | + |
| 70 | +## Iterative Test Refinement |
| 71 | + |
| 72 | +- When a test fails, analyze the provided error log to identify the root cause. |
| 73 | +- **Priority 1: Locators.** If an element is not found, correct the Playwright locator first. Prefer `getByRole`, `getByText`, `data-testid` in that order. |
| 74 | +- **Priority 2: Timing.** If the locator is correct but the element is not ready, add or adjust `page.waitForSelector` or other appropriate `waitFor` functions. Do not use fixed `page.waitForTimeout()`. |
| 75 | +- **Priority 3: Assertions.** Ensure the assertion (`expect()`) matches the actual state of the application described in the error log. |
| 76 | +- Modify the minimum necessary code to fix the specific error. Do not refactor unrelated code. |
| 77 | + |
| 78 | +## Accessibility |
| 79 | + |
| 80 | +- Include basic accessibility checks (role/label presence, keyboard navigation). |
| 81 | +- Ensure focus management in modals/menus is tested. |
| 82 | + |
| 83 | +## Misc |
| 84 | + |
| 85 | +- Keep tests fast/deterministic. Avoid real timeouts/network. |
| 86 | +- Use Jest's timer mocking (`jest.useFakeTimers`) if time-dependent. |
| 87 | +- Mock D3.js and other heavy dependencies appropriately. |
| 88 | +- Use `testPathIgnorePatterns` to exclude build artifacts. |
| 89 | +- Ensure focus management in modals/menus is tested. |
0 commit comments