-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathjest.setup.js
More file actions
44 lines (42 loc) · 1.62 KB
/
Copy pathjest.setup.js
File metadata and controls
44 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Polyfill TextEncoder/TextDecoder for supertest under Node 18+
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// --- jsdom polyfills for component/snapshot tests ---
// jsdom omits these browser APIs that MUI and a few components reach for; stub
// them (inert) so component renders don't crash in tests.
if (typeof window !== 'undefined') {
if (typeof window.matchMedia !== 'function') {
window.matchMedia = query => ({
matches: false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
});
}
// jsdom defines window.scrollTo as a stub that throws "Not implemented", so
// override it unconditionally with an inert no-op.
window.scrollTo = () => {};
if (typeof window.HTMLElement !== 'undefined') {
window.HTMLElement.prototype.scrollIntoView = () => {};
// autoFocus toggles MUI focus classes, and jsdom applies it inconsistently
// across environments (focuses locally but not on CI), which would make
// focus-sensitive snapshots flaky. Neutralize focus so the rendered markup
// is the unfocused state everywhere.
window.HTMLElement.prototype.focus = () => {};
}
}
class MockObserver {
observe() {}
unobserve() {}
disconnect() {}
takeRecords() {
return [];
}
}
if (typeof global.ResizeObserver === 'undefined') global.ResizeObserver = MockObserver;
if (typeof global.IntersectionObserver === 'undefined') global.IntersectionObserver = MockObserver;