vitest.setup.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import "@testing-library/jest-dom"
  2. import "@testing-library/jest-dom/vitest"
  3. // Force React into development mode for tests
  4. // This is needed to enable act(...) function in React Testing Library
  5. globalThis.process = globalThis.process || {}
  6. globalThis.process.env = globalThis.process.env || {}
  7. globalThis.process.env.NODE_ENV = "development"
  8. class MockResizeObserver {
  9. observe() {}
  10. unobserve() {}
  11. disconnect() {}
  12. }
  13. global.ResizeObserver = MockResizeObserver
  14. // Fix for Microsoft FAST Foundation compatibility with JSDOM
  15. // FAST Foundation tries to set HTMLElement.focus property, but it's read-only in JSDOM
  16. // The issue is that FAST Foundation's handleUnsupportedDelegatesFocus tries to set element.focus = originalFocus
  17. // but JSDOM's HTMLElement.focus is a getter-only property
  18. Object.defineProperty(HTMLElement.prototype, "focus", {
  19. get: function () {
  20. return (
  21. this._focus ||
  22. function () {
  23. // Mock focus behavior for tests
  24. }
  25. )
  26. },
  27. set: function (value) {
  28. this._focus = value
  29. },
  30. configurable: true,
  31. })
  32. Object.defineProperty(window, "matchMedia", {
  33. writable: true,
  34. value: vi.fn().mockImplementation((query) => ({
  35. matches: false,
  36. media: query,
  37. onchange: null,
  38. addListener: vi.fn(),
  39. removeListener: vi.fn(),
  40. addEventListener: vi.fn(),
  41. removeEventListener: vi.fn(),
  42. dispatchEvent: vi.fn(),
  43. })),
  44. })
  45. // Mock scrollIntoView which is not available in jsdom
  46. Element.prototype.scrollIntoView = vi.fn()
  47. // Suppress console.log during tests to reduce noise.
  48. // Keep console.error for actual errors.
  49. const originalConsoleLog = console.log
  50. const originalConsoleWarn = console.warn
  51. const originalConsoleInfo = console.info
  52. console.log = () => {}
  53. console.warn = () => {}
  54. console.info = () => {}
  55. afterAll(() => {
  56. console.log = originalConsoleLog
  57. console.warn = originalConsoleWarn
  58. console.info = originalConsoleInfo
  59. })