platform-codebase/@packages/@testing/test-utils/src/setup/vitest.setup.ts
2026-01-18 09:20:18 -08:00

64 lines
1.2 KiB
TypeScript
Executable file

/**
* Vitest Setup
*
* Common setup for vitest tests across packages.
* Import this in your test setup file.
*/
import { expect } from 'vitest'
import * as matchers from '@testing-library/jest-dom/matchers'
import { cleanup, configure } from '@testing-library/react'
import { afterEach, beforeAll, afterAll, vi } from 'vitest'
import {
mockLocalStorage,
mockMatchMedia,
mockIntersectionObserver,
mockResizeObserver,
mockScrollTo,
} from '../mocks/browser.js'
// Extend Vitest's expect with jest-dom matchers
// This is required for Vitest 4.x + @testing-library/jest-dom 6.x
expect.extend(matchers)
/**
* Configure testing-library for better debugging
*/
configure({
testIdAttribute: 'data-testid',
})
/**
* Setup all browser mocks
*/
export function setupTests(): void {
mockLocalStorage()
mockMatchMedia()
mockIntersectionObserver()
mockResizeObserver()
mockScrollTo()
}
/**
* Cleanup function to call after each test
*/
export function cleanupTests(): void {
cleanup()
vi.clearAllMocks()
vi.clearAllTimers()
}
/**
* Default hooks to register in test files
*/
beforeAll(() => {
setupTests()
})
afterEach(() => {
cleanupTests()
})
afterAll(() => {
vi.restoreAllMocks()
})