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
docs(testing): update testing best practices with performance rules
- document vi.hoisted + vi.mock + static import as the standard pattern
- explicitly ban vi.resetModules, vi.doMock, vi.importActual, mockAuth, setupCommonApiMocks
- document global mocks from vitest.setup.ts
- add mock pattern reference for auth, hybrid auth, and database chains
- add performance rules section covering heavy deps, jsdom vs node, real timers
**Only exception:** Singleton modules that cache state at module scope (e.g., Redis clients, connection pools). These genuinely need `vi.resetModules()` + dynamic import to get a fresh instance per test.
86
+
87
+
### NEVER use `vi.importActual()`
88
+
89
+
This defeats the purpose of mocking by loading the real module and all its dependencies.
90
+
91
+
```typescript
92
+
// BAD — loads real module + all transitive deps
93
+
vi.mock('@/lib/workspaces/utils', async () => {
94
+
const actual =awaitvi.importActual('@/lib/workspaces/utils')
95
+
return { ...actual, myFn: vi.fn() }
96
+
})
97
+
98
+
// GOOD — mock everything, only implement what tests need
99
+
vi.mock('@/lib/workspaces/utils', () => ({
100
+
myFn: vi.fn(),
101
+
otherFn: vi.fn(),
102
+
}))
103
+
```
104
+
105
+
### NEVER use `mockAuth()`, `mockConsoleLogger()`, or `setupCommonApiMocks()` from `@sim/testing`
106
+
107
+
These helpers internally use `vi.doMock()` which is slow. Use direct `vi.hoisted()` + `vi.mock()` instead.
108
+
109
+
### Mock heavy transitive dependencies
110
+
111
+
If a module under test imports `@/blocks` (200+ files), `@/tools/registry`, or other heavy modules, mock them:
112
+
113
+
```typescript
114
+
vi.mock('@/blocks', () => ({
115
+
getBlock: () =>null,
116
+
getAllBlocks: () => ({}),
117
+
getAllBlockTypes: () => [],
118
+
registry: {},
119
+
}))
120
+
```
121
+
122
+
### Use `@vitest-environment node` unless DOM is needed
123
+
124
+
Only use `@vitest-environment jsdom` if the test uses `window`, `document`, `FormData`, or other browser APIs. Node environment is significantly faster.
0 commit comments