( target?: string | RegExp | Array<string | RegExp>, expectOrder = true, )
| 305 | } |
| 306 | |
| 307 | async function untilBrowserLog( |
| 308 | target?: string | RegExp | Array<string | RegExp>, |
| 309 | expectOrder = true, |
| 310 | ): Promise<string[]> { |
| 311 | const { promise, resolve, reject } = promiseWithResolvers<void>() |
| 312 | let timeoutId: ReturnType<typeof setTimeout> |
| 313 | |
| 314 | const logs = [] |
| 315 | |
| 316 | try { |
| 317 | const isMatch = (matcher: string | RegExp) => (text: string) => |
| 318 | typeof matcher === 'string' ? text === matcher : matcher.test(text) |
| 319 | |
| 320 | let processMsg: (text: string) => boolean |
| 321 | |
| 322 | if (!target) { |
| 323 | processMsg = () => true |
| 324 | } else if (Array.isArray(target)) { |
| 325 | if (expectOrder) { |
| 326 | const remainingTargets = [...target] |
| 327 | processMsg = (text: string) => { |
| 328 | const nextTarget = remainingTargets.shift() |
| 329 | expect(text).toMatch(nextTarget) |
| 330 | return remainingTargets.length === 0 |
| 331 | } |
| 332 | } else { |
| 333 | const remainingMatchers = target.map(isMatch) |
| 334 | processMsg = (text: string) => { |
| 335 | const nextIndex = remainingMatchers.findIndex((matcher) => |
| 336 | matcher(text), |
| 337 | ) |
| 338 | if (nextIndex >= 0) { |
| 339 | remainingMatchers.splice(nextIndex, 1) |
| 340 | } |
| 341 | return remainingMatchers.length === 0 |
| 342 | } |
| 343 | } |
| 344 | } else { |
| 345 | processMsg = isMatch(target) |
| 346 | } |
| 347 | |
| 348 | const handleMsg = (msg: ConsoleMessage) => { |
| 349 | try { |
| 350 | const text = msg.text() |
| 351 | logs.push(text) |
| 352 | const done = processMsg(text) |
| 353 | if (done) { |
| 354 | resolve() |
| 355 | } |
| 356 | } catch (err) { |
| 357 | reject(err) |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | timeoutId = setTimeout(() => { |
| 362 | const nextTarget = Array.isArray(target) |
| 363 | ? expectOrder |
| 364 | ? target[0] |
no test coverage detected