|
| 1 | +# Jest to Vitest Migration Guide |
| 2 | + |
| 3 | +This guide covers converting Jest tests to Vitest. **Note: This guide may be incomplete** - please update it as you discover additional migration patterns. |
| 4 | + |
| 5 | +## Important: Test Commands |
| 6 | + |
| 7 | +- **Jest tests**: Continue to run with `npm run test` (`.test.tsx` files) |
| 8 | +- **Vitest tests**: Run with `npm run test:vitest` (`.vitest.tsx` files) |
| 9 | + |
| 10 | +Both test suites should continue to pass during the migration period. |
| 11 | + |
| 12 | +## Quick Migration Steps |
| 13 | + |
| 14 | +### 1. Rename Files |
| 15 | +```bash |
| 16 | +mv ComponentName.test.tsx ComponentName.vitest.tsx |
| 17 | +``` |
| 18 | + |
| 19 | +### 2. Update Imports |
| 20 | + |
| 21 | +**Before:** |
| 22 | +```typescript |
| 23 | +import { it, expect } from "@jest/globals"; |
| 24 | +import { axe } from "jest-axe"; |
| 25 | +import { setupJestCanvasMock } from "jest-canvas-mock"; |
| 26 | +``` |
| 27 | + |
| 28 | +**After:** |
| 29 | +```typescript |
| 30 | +import { it, expect, vi } from "vitest"; |
| 31 | +import { axe } from "vitest-axe"; |
| 32 | +import type { MockedFunction } from "vitest"; |
| 33 | +``` |
| 34 | + |
| 35 | +### 3. Replace Mocking |
| 36 | + |
| 37 | +**Before:** |
| 38 | +```typescript |
| 39 | +jest.mock("../path/to/module"); |
| 40 | +jest.fn() |
| 41 | +jest.spyOn(console, "error").mockImplementation(() => undefined); |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + setupJestCanvasMock(); |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +**After:** |
| 49 | +```typescript |
| 50 | +vi.mock("../path/to/module"); |
| 51 | +vi.fn() |
| 52 | + |
| 53 | +const consoleErrorSpy: MockedFunction<typeof console.error> = vi |
| 54 | + .spyOn(console, "error") |
| 55 | + .mockImplementation(() => undefined); |
| 56 | + |
| 57 | +// Remove setupJestCanvasMock - it's automatic in Vitest |
| 58 | +// Remember to restore spies: consoleErrorSpy.mockRestore(); |
| 59 | +``` |
| 60 | + |
| 61 | +### 4. Update Mock Types |
| 62 | + |
| 63 | +**Before:** |
| 64 | +```typescript |
| 65 | +const mockFn = useTelemetry as () => (...args) => void; |
| 66 | +const mockedUseSession = useSession as jest.Mock; |
| 67 | +``` |
| 68 | + |
| 69 | +**After:** |
| 70 | +```typescript |
| 71 | +const mockFn = useTelemetry as MockedFunction<() => (...args) => void>; |
| 72 | +const mockedUseSession = useSession as MockedFunction<typeof useSession>; |
| 73 | +``` |
| 74 | + |
| 75 | +## Configuration Notes |
| 76 | + |
| 77 | +- **File Pattern**: `**/src/**/*.vitest.{ts,tsx}` |
| 78 | +- **Auto Mocks**: Next.js components (`next/image`, `next/font/*`) are pre-configured |
| 79 | +- **Canvas Mock**: Automatically enabled (no manual setup needed) |
| 80 | +- **Coverage**: V8 provider with 100% thresholds |
| 81 | + |
| 82 | +## Verification |
| 83 | + |
| 84 | +After migration: |
| 85 | +1. Run `npm run test:vitest -- YourTest.vitest.tsx` |
| 86 | +2. Ensure original Jest tests still pass with `npm run test` |
| 87 | +3. Check mocks work correctly |
| 88 | + |
| 89 | +## Common Issues |
| 90 | + |
| 91 | +- File must use `.vitest.tsx` extension |
| 92 | +- Don't forget `mockRestore()` for spies |
| 93 | +- Canvas mock is automatic - remove manual setup |
| 94 | +- Update Jest references in comments to Vitest |
0 commit comments