You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/TESTING.md
+46-20Lines changed: 46 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,23 +15,30 @@ Do NOT write all test cases first and then implement everything at once.
15
15
16
16
| Layer | Tool | Scope |
17
17
|-------|------|-------|
18
-
| Unit / Component |Jest + React Testing Library | Hooks, services, component rendering, form logic |
18
+
| Unit / Component |Vitest + React Testing Library | Hooks, services, component rendering, form logic |
19
19
| E2e / Feature validation | Cypress | Validate features.json entries in real browser |
20
20
| API mocking | MSW (Mock Service Worker) | GitHub API + K8s API — mock everything first, real cluster later |
21
21
22
22
## Mock Strategy
23
23
24
-
Use MSW for all API mocking (GitHub + K8s). If SDK hooks require module-level mocks in unit tests (because they depend on Console runtime internals), fall back to Jest mocks — but try MSW first.
24
+
MSW is the primary mocking strategy for anything that hits the network (GitHub API, K8s API, Go backend). K8s API mocking uses MSW WebSocket capability.
25
25
26
-
-**Start:** Mock everything via MSW (GitHub API, K8s API)
27
-
-**Later:** Real cluster for e2e — SDK hooks work natively, GitHub API remains mocked
28
-
-**Fallback:** If SDK hooks can't be driven by MSW alone in unit tests, use Jest module mocks
26
+
`vi.mock` is only for framework and library internals that have no external service:
27
+
-`react-i18next` (translation hook)
28
+
-`@openshift-console/dynamic-plugin-sdk` (console shell runtime components like DocumentTitle, ListPageHeader, consoleFetchJSON)
29
+
-`@patternfly/react-icons` (UI library)
30
+
-`react-router-dom-v5-compat` (framework routing)
31
+
-`libsodium-wrappers` (WASM crypto library)
32
+
33
+
If it makes an HTTP or WebSocket call, mock it with MSW, not `vi.mock`.
29
34
30
35
## File Conventions
31
36
32
37
| Type | Location |
33
38
|------|----------|
34
-
| Unit / Component tests |`src/**/*.test.ts\|tsx`|
| User flows | E2e | Create form → submit → list shows new function |
47
54
55
+
## Component vs. Page Tests
56
+
57
+
Every component gets its own exhaustive test file. Every page gets its own test file that tests the page's orchestration and integration with its components.
58
+
59
+
**Component tests** cover:
60
+
- Rendering based on props (all states and variants)
61
+
- User interactions that trigger callbacks (clicks, input, form validation)
62
+
- Internal state (expand/collapse, selection)
63
+
64
+
**Page tests** cover:
65
+
- Component is present on the page and wired correctly
66
+
- Data flows from hooks/services to components (correct props)
67
+
- User actions that trigger cross-component effects or service calls (e.g., form submit calls service, then navigates)
68
+
- Page-level states: loading, error, empty
69
+
70
+
Overlap between component tests and page tests is expected and acceptable. They test at different levels: component tests verify the component works in isolation, page tests verify the page's orchestration logic works correctly.
71
+
48
72
## Testing Best Practices
49
73
50
74
1.**User-Centric Testing** — Test what users see and interact with.
@@ -63,48 +87,50 @@ Use MSW for all API mocking (GitHub + K8s). If SDK hooks require module-level mo
63
87
64
88
## Mocking Patterns
65
89
90
+
MSW is the primary approach. `vi.mock` is rare (see Mock Strategy above).
91
+
66
92
Use ESM `import` at top of file. Never use `require('react')` or `React.createElement()` in mocks.
67
-
Prefer `jest.mock()` for modules, `jest.fn()` for components. Keep mocks simple.
93
+
Keep mocks simple.
68
94
69
-
**Correct patterns:**
95
+
**Correct patterns (for the rare `vi.mock` cases):**
0 commit comments