-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
42 lines (35 loc) · 1.74 KB
/
vitest.setup.ts
File metadata and controls
42 lines (35 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { afterEach, vi } from 'vitest';
import '@testing-library/jest-dom/vitest'; // Provides helpful DOM assertion matchers (like .toBeInTheDocument) via the 'vitest' export
// --- Explanation ---
// This file runs once before all tests.
// It's the ideal place for global setup, mocks, or importing libraries that extend Vitest's capabilities.
// --- Common Use Cases ---
// 1. Extending `expect` with custom matchers:
// The import '@testing-library/jest-dom/vitest' above is a prime example.
// 2. Global Mocks:
// If you need to mock global objects like `fetch`, `localStorage`, etc., for all tests.
// Example (commented out):
// vi.mock('axios'); // Mock axios globally (implement mock in __mocks__/axios.ts)
/*
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => { store[key] = value.toString(); },
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
};
})();
vi.stubGlobal('localStorage', localStorageMock);
vi.stubGlobal('sessionStorage', localStorageMock); // Often useful to mock both
*/
// 3. Cleaning up between tests:
// Ensures mocks or DOM changes don't leak between tests.
// Example: Clearing all mocks after each test
afterEach(() => {
vi.clearAllMocks(); // Clears call history, reset mock implementations added with vi.fn().mockImplementation()
// vi.resetAllMocks(); // Resets mocks to their initial state (removes implementations, clears history) - choose one based on need
});
// --- Project-Specific Setup ---
// Add any setup specific to react-api-kit here as needed later.
console.log('🚀 Vitest Global Setup: Loaded'); // Optional log to confirm execution