MCPcopy
hub / github.com/vitejs/vite / untilConsoleLog

Function untilConsoleLog

playground/hmr-ssr/__tests__/hmr-ssr.spec.ts:995–1074  ·  view source on GitHub ↗
(
  target?: string | RegExp | Array<string | RegExp>,
  expectOrder = true,
)

Source from the content-addressed store, hash-verified

993}
994
995async function untilConsoleLog(
996 target?: string | RegExp | Array<string | RegExp>,
997 expectOrder = true,
998): Promise<string[]> {
999 const { promise, resolve, reject } = promiseWithResolvers<void>()
1000 let timeoutId: ReturnType<typeof setTimeout>
1001
1002 const logsMessages = []
1003
1004 try {
1005 const isMatch = (matcher: string | RegExp) => (text: string) =>
1006 typeof matcher === 'string' ? text === matcher : matcher.test(text)
1007
1008 let processMsg: (text: string) => boolean
1009
1010 if (!target) {
1011 processMsg = () => true
1012 } else if (Array.isArray(target)) {
1013 if (expectOrder) {
1014 const remainingTargets = [...target]
1015 processMsg = (text: string) => {
1016 const nextTarget = remainingTargets.shift()
1017 expect(text).toMatch(nextTarget)
1018 return remainingTargets.length === 0
1019 }
1020 } else {
1021 const remainingMatchers = target.map(isMatch)
1022 processMsg = (text: string) => {
1023 const nextIndex = remainingMatchers.findIndex((matcher) =>
1024 matcher(text),
1025 )
1026 if (nextIndex >= 0) {
1027 remainingMatchers.splice(nextIndex, 1)
1028 }
1029 return remainingMatchers.length === 0
1030 }
1031 }
1032 } else {
1033 processMsg = isMatch(target)
1034 }
1035
1036 const handleMsg = (text: string) => {
1037 try {
1038 text = text.replace(/\n$/, '')
1039 logsMessages.push(text)
1040 const done = processMsg(text)
1041 if (done) {
1042 resolve()
1043 logsEmitter.off('log', handleMsg)
1044 }
1045 } catch (err) {
1046 reject(err)
1047 logsEmitter.off('log', handleMsg)
1048 }
1049 }
1050
1051 timeoutId = setTimeout(() => {
1052 const nextTarget = Array.isArray(target)

Callers 1

untilConsoleLogAfterFunction · 0.85

Calls 4

isMatchFunction · 0.70
offMethod · 0.65
onMethod · 0.65
promiseWithResolversFunction · 0.50

Tested by

no test coverage detected