Repository navigation
Expand file tree
/
Copy pathtest-setup.ts
More file actions
147 lines (136 loc) · 4.48 KB
/
Copy pathtest-setup.ts
File metadata and controls
147 lines (136 loc) · 4.48 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable @typescript-eslint/unbound-method */
// context: https://github.com/testing-library/jest-dom/pull/511
import "@testing-library/jest-dom/vitest"
import { cleanup } from "@testing-library/react"
import { afterAll, afterEach, beforeAll } from "vitest"
// Make TypeScript aware of Node.js global object
declare const global: typeof globalThis & {
IS_REACT_ACT_ENVIRONMENT?: boolean
matchMedia?: (query: string) => {
addListener: (listener: () => void) => void
removeListener: (listener: () => void) => void
}
}
global.IS_REACT_ACT_ENVIRONMENT = true
global.matchMedia =
global.matchMedia ||
function () {
return {
addListener: () => undefined,
removeListener: () => undefined,
}
}
// Suppress React act() warnings from Ant Design components
// These warnings come from Ant Design's internal portal/dropdown components
// and are not actionable since they originate from third-party library code
const originalConsoleError = console.error
beforeAll(() => {
console.error = (...args: unknown[]) => {
const message = args[0]
if (
typeof message === "string" &&
message.includes("was not wrapped in act")
) {
return
}
originalConsoleError.apply(console, args)
}
})
afterAll(() => {
console.error = originalConsoleError
})
// Workaround for jsdom/nwsapi invalid CSS selector parsing issue with Ant Design
// This patches querySelector and matches to handle invalid selectors gracefully
// The issue occurs when Ant Design generates CSS selectors with multiple consecutive commas
if (typeof document !== "undefined") {
const originalQuerySelector = Element.prototype.querySelector
const originalQuerySelectorAll = Element.prototype.querySelectorAll
const originalMatches = Element.prototype.matches
// Check if selector has invalid patterns (multiple consecutive commas)
const hasInvalidSelectorPattern = (selector: string): boolean => {
if (!selector || typeof selector !== "string") {
return false
}
// Check for multiple consecutive commas which cause nwsapi parsing errors
// This is the main pattern causing issues: selectors like "input,,,properties"
return /,{2,}/.test(selector)
}
Element.prototype.querySelector = function (
this: Element,
selector: string,
): Element | null {
// Always check for invalid patterns first
if (hasInvalidSelectorPattern(selector)) {
return null
}
// Wrap in try-catch to handle any parsing errors from nwsapi
try {
return originalQuerySelector.call(this, selector)
} catch (e: unknown) {
// Silently handle SyntaxError from nwsapi parser
const error = e as Error
if (
error instanceof SyntaxError ||
error?.name === "SyntaxError" ||
error?.message?.includes("not a valid selector")
) {
return null
}
throw e
}
}
Element.prototype.querySelectorAll = function (
this: Element,
selector: string,
): NodeListOf<Element> {
// Always check for invalid patterns first
if (hasInvalidSelectorPattern(selector)) {
// Return empty NodeList by creating a fragment and querying an impossible selector
const fragment = document.createDocumentFragment()
return fragment.querySelectorAll(":not(*)")
}
// Wrap in try-catch to handle any parsing errors from nwsapi
try {
return originalQuerySelectorAll.call(this, selector)
} catch (e: unknown) {
// Silently handle SyntaxError from nwsapi parser
const error = e as Error
if (
error instanceof SyntaxError ||
error?.name === "SyntaxError" ||
error?.message?.includes("not a valid selector")
) {
const fragment = document.createDocumentFragment()
return fragment.querySelectorAll(":not(*)")
}
throw e
}
}
Element.prototype.matches = function (
this: Element,
selector: string,
): boolean {
// Always check for invalid patterns first
if (hasInvalidSelectorPattern(selector)) {
return false
}
// Wrap in try-catch to handle any parsing errors from nwsapi
try {
return originalMatches.call(this, selector)
} catch (e: unknown) {
// Silently handle SyntaxError from nwsapi parser
const error = e as Error
if (
error instanceof SyntaxError ||
error?.name === "SyntaxError" ||
error?.message?.includes("not a valid selector")
) {
return false
}
throw e
}
}
}
afterEach(() => {
cleanup()
})