|
4 | 4 | printColorsInErrorMessages, |
5 | 5 | removeFuncFromStackTrace, |
6 | 6 | } from './utils'; |
7 | | -import type { ElementHandle, JSHandle } from 'puppeteer'; |
| 7 | +import type { ElementHandle, JSHandle, Page } from 'puppeteer'; |
8 | 8 | import { createClientRuntimeServer } from './module-server/client-runtime-server'; |
9 | 9 | import type { AsyncHookTracker } from './async-hooks'; |
10 | 10 |
|
@@ -216,3 +216,95 @@ export const getQueriesForElement = ( |
216 | 216 |
|
217 | 217 | return queries; |
218 | 218 | }; |
| 219 | + |
| 220 | +let waitForCounter = 0; |
| 221 | + |
| 222 | +export interface WaitForOptions { |
| 223 | + /** |
| 224 | + * The element watched by the MutationObserver which, |
| 225 | + * when it or its descendants change, |
| 226 | + * causes the callback to run again (regardless of the interval). |
| 227 | + * Default: `document.documentElement` (root element) |
| 228 | + */ |
| 229 | + container?: ElementHandle; |
| 230 | + /** |
| 231 | + * The amount of time (milliseconds) that will pass before waitFor "gives up" and throws whatever the callback threw. |
| 232 | + * Default: 1000ms |
| 233 | + */ |
| 234 | + timeout?: number; |
| 235 | + /** |
| 236 | + * The maximum amount of time (milliseconds) that will pass between each run of the callback. |
| 237 | + * If the MutationObserver notices a DOM change before this interval triggers, |
| 238 | + * the callback will run again immediately. |
| 239 | + * Default: 50ms |
| 240 | + */ |
| 241 | + interval?: number; |
| 242 | + /** Manipulate the error thrown when the timeout triggers. */ |
| 243 | + onTimeout?: (error: Error) => Error; |
| 244 | + /** Options to pass to initialize the MutationObserver. */ |
| 245 | + mutationObserverOptions?: MutationObserverInit; |
| 246 | +} |
| 247 | + |
| 248 | +interface WaitFor { |
| 249 | + <T>( |
| 250 | + page: Page, |
| 251 | + asyncHookTracker: AsyncHookTracker, |
| 252 | + cb: () => T | Promise<T>, |
| 253 | + { onTimeout, container, ...opts }: WaitForOptions, |
| 254 | + wrappedFunction: (...args: any) => any, |
| 255 | + ): Promise<T>; |
| 256 | +} |
| 257 | + |
| 258 | +export const waitFor: WaitFor = async ( |
| 259 | + page, |
| 260 | + asyncHookTracker, |
| 261 | + cb, |
| 262 | + { onTimeout, container, ...opts }, |
| 263 | + wrappedFunction, |
| 264 | +) => |
| 265 | + asyncHookTracker.addHook(async () => { |
| 266 | + const { port } = await createClientRuntimeServer(); |
| 267 | + |
| 268 | + waitForCounter++; |
| 269 | + // Functions exposed via page.exposeFunction can't be removed, |
| 270 | + // So we need a unique name for each variable |
| 271 | + const browserFuncName = `pleasantest_waitFor_${waitForCounter}`; |
| 272 | + |
| 273 | + await page.exposeFunction(browserFuncName, cb); |
| 274 | + |
| 275 | + const evalResult = await page.evaluateHandle( |
| 276 | + // Using new Function to avoid babel transpiling the import |
| 277 | + // @ts-expect-error pptr's types don't like new Function |
| 278 | + new Function( |
| 279 | + 'opts', |
| 280 | + 'container', |
| 281 | + `return import("http://localhost:${port}/@pleasantest/dom-testing-library") |
| 282 | + .then(async ({ waitFor }) => { |
| 283 | + try { |
| 284 | + const result = await waitFor(${browserFuncName}, { ...opts, container }) |
| 285 | + return { success: true, result } |
| 286 | + } catch (error) { |
| 287 | + if (/timed out in waitFor/i.test(error.message)) { |
| 288 | + // Leave out stack trace so the stack trace is given from Node |
| 289 | + return { success: false, result: { message: error.message } } |
| 290 | + } |
| 291 | + return { success: false, result: error } |
| 292 | + } |
| 293 | + })`, |
| 294 | + ), |
| 295 | + opts, |
| 296 | + // Container has to be passed separately because puppeteer won't unwrap nested JSHandles |
| 297 | + container, |
| 298 | + ); |
| 299 | + const wasSuccessful = await evalResult.evaluate((r) => r.success); |
| 300 | + const result = await evalResult.evaluate((r) => |
| 301 | + r.success |
| 302 | + ? r.result |
| 303 | + : { message: r.result.message, stack: r.result.stack }, |
| 304 | + ); |
| 305 | + if (wasSuccessful) return result; |
| 306 | + const err = new Error(result.message); |
| 307 | + if (result.stack) err.stack = result.stack; |
| 308 | + else removeFuncFromStackTrace(err, asyncHookTracker.addHook); |
| 309 | + throw onTimeout ? onTimeout(err) : err; |
| 310 | + }, wrappedFunction); |
0 commit comments