forked from Comfy-Org/ComfyUI_frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
174 lines (160 loc) · 5.44 KB
/
Copy pathvitest.setup.ts
File metadata and controls
174 lines (160 loc) · 5.44 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import '@testing-library/jest-dom/vitest'
import { createTestingPinia } from '@pinia/testing'
import { setActivePinia } from 'pinia'
import { beforeEach, vi } from 'vitest'
import 'vue'
import DOMPurify from 'dompurify'
beforeEach(() => {
setActivePinia(createTestingPinia({ stubActions: false }))
})
/**
* Unit tests must never perform real network I/O.
*
* happy-dom serves the page from `http://localhost:3000` (vitest's default
* environment URL), so any un-mocked relative `fetch('/api/...')` becomes a
* real TCP connection to a port nothing is listening on. The request outlives
* the test that started it, and when it finally fails the resulting
* `console.error` is emitted after vitest has torn the environment down:
*
* EnvironmentTeardownError: [vitest-worker]: Closing rpc while
* "onUserConsoleLog" was pending
*
* Vitest counts that as an unhandled error and exits non-zero even when every
* test passed, which silently evicts PRs from the merge queue.
*
* Rejecting immediately keeps the failure inside the test that caused it,
* where it is attributable and fixable. Tests that need network behaviour must
* mock the module that issues the request, or stub `fetch` themselves - a
* local stub replaces this guard.
*/
const originalFetch = globalThis.fetch
function resolveRequestUrl(input: RequestInfo | URL): string {
const requestedUrl =
typeof input === 'string'
? input
: input instanceof URL
? input.href
: input.url
try {
return new URL(requestedUrl, globalThis.location?.href).href
} catch {
return requestedUrl
}
}
const blockedNetworkFetch: typeof globalThis.fetch = (input, init) => {
const requestUrl = resolveRequestUrl(input)
if (!/^https?:/i.test(requestUrl)) {
return originalFetch(input, init)
}
return Promise.reject(
new Error(
`Blocked a real network request to ${requestUrl} from a unit test. ` +
'Mock the module that issues it (or stub globalThis.fetch in the ' +
'test) instead of letting the request escape - see vitest.setup.ts.'
)
)
}
globalThis.fetch = blockedNetworkFetch
// Mock @sparkjsdev/spark which uses WASM that doesn't work in Node.js
vi.mock('@sparkjsdev/spark', async () => {
const three = await import('three')
return {
SplatMesh: class SplatMesh {
constructor() {}
},
SparkRenderer: class SparkRenderer extends three.Object3D {
constructor() {
super()
}
}
}
})
// Augment Window interface for tests
declare global {
interface Window {
__CONFIG__: {
mixpanel_token?: string
require_whitelist?: boolean
subscription_required?: boolean
max_upload_size?: number
comfy_api_base_url?: string
comfy_platform_base_url?: string
firebase_config?: {
apiKey: string
authDomain: string
databaseURL?: string
projectId: string
storageBucket: string
messagingSenderId: string
appId: string
measurementId?: string
}
server_health_alert?: {
message: string
tooltip?: string
severity?: 'info' | 'warning' | 'error'
badge?: string
}
}
}
}
// Define global variables for tests
globalThis.__COMFYUI_FRONTEND_VERSION__ = '1.24.0'
globalThis.__SENTRY_DSN__ = ''
globalThis.__ALGOLIA_APP_ID__ = ''
globalThis.__ALGOLIA_API_KEY__ = ''
globalThis.__USE_PROD_CONFIG__ = false
globalThis.__DISTRIBUTION__ = 'localhost'
globalThis.__IS_NIGHTLY__ = false
// Define runtime config for tests (absent in @vitest-environment node files)
if (globalThis.window) {
window.__CONFIG__ = {
subscription_required: true,
mixpanel_token: 'test-token',
comfy_api_base_url: 'https://stagingapi.comfy.org',
comfy_platform_base_url: 'https://stagingplatform.comfy.org',
firebase_config: {
apiKey: 'test',
authDomain: 'test.firebaseapp.com',
projectId: 'test',
storageBucket: 'test.appspot.com',
messagingSenderId: '123',
appId: '123'
}
}
}
// Mock Worker for extendable-media-recorder
globalThis.Worker = vi.fn(function () {
return {
postMessage: vi.fn(),
terminate: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn()
}
})
// Tripwire: dompurify >= 3.4.8 is inert under happy-dom.
// Root cause (capricorn86/happy-dom#2182, FE-1189): happy-dom's
// Node.prototype.nodeName getter returns '' when invoked directly on an
// element. dompurify 3.4.8 started reading tag names through that
// prototype getter as a DOM-clobbering defense, so under happy-dom every
// node gets an empty tag name and the ALLOWED_TAGS logic misfires in both
// directions: allowed wrappers are dropped and disallowed content
// (<script>, javascript: URLs) survives. jsdom implements the getter
// correctly; dompurify output there matches the pre-3.4.8 control.
// Sanitizer tests therefore run under jsdom (per-file pragma), and this
// wrapper makes it impossible to assert against the inert sanitizer
// silently: throwing on call (not on load) leaves the ~16k tests that
// never sanitize unaffected.
if (
typeof DOMPurify?.sanitize === 'function' &&
DOMPurify.sanitize('<h1>x</h1>') !== '<h1>x</h1>'
) {
DOMPurify.sanitize = (() => {
throw new Error(
'DOMPurify.sanitize is inert in this test environment (happy-dom ' +
'Node.prototype.nodeName bug — see vitest.setup.ts). Add ' +
'`// @vitest-environment jsdom` to this test file.'
)
}) as typeof DOMPurify.sanitize
}