create-fluxstack
Version:
⚡ Revolutionary full-stack TypeScript framework with Declarative Config System, Elysia + React + Bun
70 lines (61 loc) • 1.66 kB
text/typescript
/**
* Test Setup
* Global test configuration and mocks
*/
import { expect, afterEach, vi } from 'vitest'
import { cleanup } from '@testing-library/react'
import * as matchers from '@testing-library/jest-dom/matchers'
// Extend Vitest's expect with jest-dom matchers
expect.extend(matchers)
// Mock fetch with proper typing
global.fetch = vi.fn() as any
// Cleanup after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup()
})
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
// Mock window.ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}))
// Mock localStorage
const localStorageMock = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
}
vi.stubGlobal('localStorage', localStorageMock)
// Mock sessionStorage
const sessionStorageMock = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
}
vi.stubGlobal('sessionStorage', sessionStorageMock)
// Mock console methods to reduce noise in tests
global.console = {
...console,
// Uncomment to ignore console logs in tests
// log: vi.fn(),
// debug: vi.fn(),
// info: vi.fn(),
// warn: vi.fn(),
// error: vi.fn(),
}