-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
92 lines (84 loc) · 2.94 KB
/
Copy pathvitest.setup.ts
File metadata and controls
92 lines (84 loc) · 2.94 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
// Global test setup for jsdom environments.
//
// jsdom deliberately does NOT implement layout APIs (ResizeObserver,
// Element.scrollIntoView, etc.) because it has no real layout engine.
// Components under test (virtual scroll, auto-grow textareas, terminal panels,
// auto-scroll-to-bottom message viewers) rely on these. Without polyfills they
// throw "X is not defined" / "not a function" and the test never reaches the
// assertion. These are inert no-op shims — enough for React effects to run.
import { afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
// @testing-library/react auto-registers cleanup via the vitest global `afterEach`
// hook — but only when `test.globals` is enabled. This project runs with
// `globals: false`, so we wire cleanup up explicitly here to prevent DOM from
// leaking between tests (which causes spurious "multiple elements" failures).
afterEach(() => {
cleanup()
})
if (typeof globalThis.ResizeObserver === 'undefined') {
class ResizeObserverStub {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}
globalThis.ResizeObserver = ResizeObserverStub as unknown as typeof ResizeObserver
}
if (typeof globalThis.IntersectionObserver === 'undefined') {
class IntersectionObserverStub {
readonly root: Element | null = null
readonly rootMargin: string = ''
readonly thresholds: ReadonlyArray<number> = []
observe(): void {}
unobserve(): void {}
disconnect(): void {}
takeRecords(): IntersectionObserverEntry[] {
return []
}
}
globalThis.IntersectionObserver =
IntersectionObserverStub as unknown as typeof IntersectionObserver
}
if (
typeof Element !== 'undefined' &&
typeof Element.prototype.scrollIntoView !== 'function'
) {
Element.prototype.scrollIntoView = function scrollIntoView(): void {
// no-op: jsdom has no layout to scroll
}
}
if (
typeof Element !== 'undefined' &&
typeof Element.prototype.scrollTo !== 'function'
) {
Element.prototype.scrollTo = function scrollTo(): void {
// no-op
}
}
// jsdom lacks window.matchMedia; some components query prefers-color-scheme etc.
if (typeof window !== 'undefined' && typeof window.matchMedia !== 'function') {
window.matchMedia = ((query: string) => ({
matches: false,
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
})) as unknown as typeof window.matchMedia
}
// Silence "not implemented" navigation errors from jsdom that flood the log
// when a component renders <a href> clicked in tests.
if (typeof window !== 'undefined') {
const origError = console.error
console.error = (...args: unknown[]) => {
const first = args[0]
if (
typeof first === 'string' &&
first.includes('Not implemented: navigation')
) {
return
}
origError(...(args as Parameters<typeof console.error>))
}
}