-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
110 lines (95 loc) · 3.45 KB
/
setup.ts
File metadata and controls
110 lines (95 loc) · 3.45 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Vitest setup file for global test configuration.
*
* This file configures Testing Library and jest-dom matchers
* for the whole monorepo, including the frontend app.
*/
import '@testing-library/jest-dom/vitest';
import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
const REQUEST_SUBMIT_NOT_IMPLEMENTED_MESSAGE = "Not implemented: HTMLFormElement's requestSubmit() method";
const originalConsoleError = console.error.bind(console);
console.error = (...args: unknown[]) => {
const first = args[0];
if (typeof first === 'string' && first.includes(REQUEST_SUBMIT_NOT_IMPLEMENTED_MESSAGE)) {
return;
}
originalConsoleError(...args);
};
const originalConsoleWarn = console.warn.bind(console);
console.warn = (...args: unknown[]) => {
const first = args[0];
if (typeof first === 'string' && first.includes(REQUEST_SUBMIT_NOT_IMPLEMENTED_MESSAGE)) {
return;
}
originalConsoleWarn(...args);
};
const originalStderrWrite: typeof process.stderr.write = process.stderr.write.bind(process.stderr);
process.stderr.write = ((
chunk: string | Uint8Array,
encodingOrCallback?: BufferEncoding | ((err?: Error | null) => void),
callback?: (err?: Error | null) => void,
): boolean => {
if (typeof chunk === 'string' && chunk.includes(REQUEST_SUBMIT_NOT_IMPLEMENTED_MESSAGE)) {
return true;
}
if (typeof chunk === 'string') {
if (typeof encodingOrCallback === 'function') {
return originalStderrWrite(chunk, encodingOrCallback);
}
return originalStderrWrite(chunk, encodingOrCallback, callback);
}
if (typeof encodingOrCallback === 'function' || encodingOrCallback === undefined) {
return originalStderrWrite(chunk, encodingOrCallback);
}
return originalStderrWrite(chunk, callback);
}) as typeof process.stderr.write;
// JSDOM doesn't implement HTMLFormElement.requestSubmit().
// Some components call it indirectly (e.g. via submit buttons), which produces noisy warnings.
if (typeof HTMLFormElement !== 'undefined') {
// Minimal polyfill for tests.
// We intentionally don't emulate full browser semantics (submitter, validation, etc.).
// Overwrite even if JSDOM provides a "not implemented" stub to avoid console noise.
HTMLFormElement.prototype.requestSubmit = function requestSubmit(): void {
this.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
};
}
/**
* Mock IntersectionObserver for tests that use react-intersection-observer
* or other visibility-tracking libraries.
*/
if (typeof window !== 'undefined') {
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | Document | null = null;
readonly rootMargin: string = '';
readonly thresholds: ReadonlyArray<number> = [];
constructor(_callback: IntersectionObserverCallback) {
// Mock implementation: do nothing
}
observe(_element: Element): void {
// Mock implementation: do nothing
}
unobserve(_element: Element): void {
// Mock implementation: do nothing
}
disconnect(): void {
// Mock implementation: do nothing
}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
}
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
Object.defineProperty(global, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
}
afterEach(() => {
cleanup();
});