setup.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // 测试环境设置函数
  2. export const setupTestEnv = () => {
  3. // 设置环境变量
  4. process.env.TARO_ENV = 'h5'
  5. process.env.TARO_PLATFORM = 'web'
  6. process.env.SUPPORT_TARO_POLYFILL = 'disabled'
  7. // 定义 defineAppConfig 全局函数用于测试 Taro 配置文件
  8. ;(global as any).defineAppConfig = (config: any) => config
  9. // 模拟 MutationObserver
  10. // @ts-ignore
  11. global.MutationObserver = class {
  12. disconnect() {}
  13. observe(_element: any, _initObject: any) {}
  14. takeRecords() { return [] }
  15. }
  16. // 模拟 IntersectionObserver
  17. // @ts-ignore
  18. global.IntersectionObserver = class {
  19. constructor(fn: (args: any[]) => void) {
  20. setTimeout(() => {
  21. fn([{ isIntersecting: true }])
  22. }, 1000)
  23. }
  24. observe() {}
  25. unobserve() {}
  26. disconnect() {}
  27. takeRecords() { return [] }
  28. root: null = null
  29. rootMargin: string = ''
  30. thresholds: number[] = []
  31. }
  32. // 模拟 ResizeObserver
  33. // @ts-ignore
  34. global.ResizeObserver = class {
  35. observe() {}
  36. unobserve() {}
  37. disconnect() {}
  38. }
  39. // 模拟 matchMedia
  40. Object.defineProperty(window, 'matchMedia', {
  41. writable: true,
  42. value: jest.fn().mockImplementation(query => ({
  43. matches: false,
  44. media: query,
  45. onchange: null,
  46. addListener: jest.fn(), // deprecated
  47. removeListener: jest.fn(), // deprecated
  48. addEventListener: jest.fn(),
  49. removeEventListener: jest.fn(),
  50. dispatchEvent: jest.fn(),
  51. })),
  52. })
  53. // 模拟 getComputedStyle
  54. Object.defineProperty(window, 'getComputedStyle', {
  55. value: () => ({
  56. getPropertyValue: (prop: string) => {
  57. return {
  58. 'font-size': '16px',
  59. 'font-family': 'Arial',
  60. color: 'rgb(0, 0, 0)',
  61. 'background-color': 'rgb(255, 255, 255)',
  62. width: '100px',
  63. height: '100px',
  64. top: '0px',
  65. left: '0px',
  66. right: '0px',
  67. bottom: '0px',
  68. x: '0px',
  69. y: '0px'
  70. }[prop] || ''
  71. }
  72. })
  73. })
  74. // 模拟 Element.prototype.getBoundingClientRect
  75. Element.prototype.getBoundingClientRect = jest.fn(() => ({
  76. width: 100,
  77. height: 100,
  78. top: 0,
  79. left: 0,
  80. bottom: 100,
  81. right: 100,
  82. x: 0,
  83. y: 0,
  84. toJSON: () => ({
  85. width: 100,
  86. height: 100,
  87. top: 0,
  88. left: 0,
  89. bottom: 100,
  90. right: 100,
  91. x: 0,
  92. y: 0
  93. })
  94. }))
  95. // 静默 console.error 在测试中
  96. const originalConsoleError = console.error
  97. console.error = (...args: any[]) => {
  98. // 检查是否在测试环境中(通过 Jest 环境变量判断)
  99. const isTestEnv = process.env.JEST_WORKER_ID !== undefined ||
  100. typeof jest !== 'undefined'
  101. // 在测试环境中静默错误输出,除非是重要错误
  102. if (isTestEnv && !args[0]?.includes?.('重要错误')) {
  103. return
  104. }
  105. originalConsoleError(...args)
  106. }
  107. }