|
import util from 'util'; |
|
import React from 'react'; |
|
import type { DOMWindow } from 'jsdom'; |
|
|
|
console.log('Current React Version:', React.version); |
|
|
|
const originConsoleErr = console.error; |
|
|
|
const ignoreWarns = [ |
|
'validateDOMNesting', |
|
'on an unmounted component', |
|
'not wrapped in act', |
|
'You called act', |
|
]; |
|
|
|
|
|
console.error = (...args) => { |
|
const str = args.join('').replace(/\n/g, ''); |
|
if (ignoreWarns.every((warn) => !str.includes(warn))) { |
|
originConsoleErr(...args); |
|
} |
|
}; |
|
|
|
type Writeable<T> = { -readonly [P in keyof T]: T[P] }; |
|
|
|
|
|
export function fillWindowEnv(window: Window | DOMWindow) { |
|
const win = window as Writeable<Window> & typeof globalThis; |
|
|
|
win.resizeTo = (width, height) => { |
|
win.innerWidth = width || win.innerWidth; |
|
win.innerHeight = height || win.innerHeight; |
|
win.dispatchEvent(new Event('resize')); |
|
}; |
|
win.scrollTo = () => {}; |
|
|
|
if (!win.matchMedia) { |
|
Object.defineProperty(win, 'matchMedia', { |
|
writable: true, |
|
configurable: true, |
|
value: jest.fn((query) => ({ |
|
matches: query.includes('max-width'), |
|
addEventListener: jest.fn(), |
|
removeEventListener: jest.fn(), |
|
})), |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
win.AnimationEvent = win.AnimationEvent || win.Event; |
|
win.TransitionEvent = win.TransitionEvent || win.Event; |
|
|
|
|
|
|
|
Object.defineProperty(win, 'TextEncoder', { writable: true, value: util.TextEncoder }); |
|
Object.defineProperty(win, 'TextDecoder', { writable: true, value: util.TextDecoder }); |
|
} |
|
|
|
if (typeof window !== 'undefined') { |
|
fillWindowEnv(window); |
|
} |
|
|
|
global.requestAnimationFrame = global.requestAnimationFrame || global.setTimeout; |
|
global.cancelAnimationFrame = global.cancelAnimationFrame || global.clearTimeout; |
|
|
|
if (typeof MessageChannel === 'undefined') { |
|
(global as any).MessageChannel = function MessageChannel() { |
|
const port1: any = {}; |
|
const port2: any = {}; |
|
port1.postMessage = port2.onmessage = () => {}; |
|
port2.postMessage = port1.onmessage = () => {}; |
|
return { port1, port2 }; |
|
}; |
|
} |
|
|
|
|
|
jest.mock('react', () => { |
|
const originReact = jest.requireActual('react'); |
|
|
|
let cloneReact = { |
|
...originReact, |
|
}; |
|
|
|
if (process.env.MOCK_USE_ID !== 'false') { |
|
cloneReact = { |
|
...cloneReact, |
|
useId: () => 'test-id', |
|
}; |
|
} |
|
|
|
return cloneReact; |
|
}); |
|
|