vitest.setup.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import '@testing-library/jest-dom'
  3. import { configure } from '@testing-library/vue'
  4. import * as matchers from 'vitest-axe/matchers'
  5. import { expect } from 'vitest'
  6. import 'vitest-axe/extend-expect'
  7. import { ServiceWorkerHelper } from '@shared/utils/testSw'
  8. global.__ = (source) => {
  9. return source
  10. }
  11. window.sw = new ServiceWorkerHelper()
  12. configure({
  13. testIdAttribute: 'data-test-id',
  14. asyncUtilTimeout: 5000,
  15. })
  16. Object.defineProperty(Element.prototype, 'scroll', { value: vi.fn() })
  17. Object.defineProperty(Element.prototype, 'scrollBy', { value: vi.fn() })
  18. Object.defineProperty(Element.prototype, 'scrollIntoView', { value: vi.fn() })
  19. require.extensions['.css'] = () => ({})
  20. vi.stubGlobal('requestAnimationFrame', (cb: () => void) => {
  21. setTimeout(cb, 0)
  22. })
  23. vi.stubGlobal('scrollTo', vi.fn())
  24. vi.stubGlobal('matchMedia', (media: string) => ({
  25. matches: false,
  26. media,
  27. onchange: null,
  28. addEventListener: vi.fn(),
  29. removeEventListener: vi.fn(),
  30. }))
  31. vi.mock('@shared/components/CommonNotifications/composable', async () => {
  32. const { default: originalUseNotifications } = await vi.importActual<any>(
  33. '@shared/components/CommonNotifications/composable',
  34. )
  35. let notifications: any
  36. const useNotifications = () => {
  37. if (notifications) return notifications
  38. const result = originalUseNotifications()
  39. notifications = {
  40. notify: vi.fn(result.notify),
  41. notifications: result.notifications,
  42. removeNotification: vi.fn(result.removeNotification),
  43. clearAllNotifications: vi.fn(result.clearAllNotifications),
  44. hasErrors: vi.fn(result.hasErrors),
  45. }
  46. return notifications
  47. }
  48. return {
  49. default: useNotifications,
  50. }
  51. })
  52. // mock vueuse because of CommonDialog, it uses usePointerSwipe
  53. // that is not supported in JSDOM
  54. vi.mock('@vueuse/core', async () => {
  55. const mod = await vi.importActual<typeof import('@vueuse/core')>(
  56. '@vueuse/core',
  57. )
  58. return {
  59. ...mod,
  60. usePointerSwipe: vi
  61. .fn()
  62. .mockReturnValue({ distanceY: 0, isSwiping: false }),
  63. }
  64. })
  65. beforeEach((context) => {
  66. context.skipConsole = false
  67. if (!vi.isMockFunction(console.warn)) {
  68. vi.spyOn(console, 'warn').mockClear()
  69. } else {
  70. vi.mocked(console.warn).mockClear()
  71. }
  72. if (!vi.isMockFunction(console.error)) {
  73. vi.spyOn(console, 'error').mockClear()
  74. } else {
  75. vi.mocked(console.error).mockClear()
  76. }
  77. })
  78. afterEach((context) => {
  79. // we don't import it from `renderComponent`, because it may renderComponent may not be called
  80. // and it doesn't make sense to import everything from it
  81. if ('cleanupComponents' in globalThis) {
  82. globalThis.cleanupComponents()
  83. }
  84. if (context.skipConsole !== true) {
  85. expect(
  86. console.warn,
  87. 'there were no warning during test',
  88. ).not.toHaveBeenCalled()
  89. expect(
  90. console.error,
  91. 'there were no errors during test',
  92. ).not.toHaveBeenCalled()
  93. }
  94. })
  95. // Import the matchers for accessibility testing with aXe.
  96. expect.extend(matchers)
  97. declare module 'vitest' {
  98. interface TestContext {
  99. skipConsole: boolean
  100. }
  101. }
  102. declare global {
  103. function cleanupComponents(): void
  104. }