MCPcopy
hub / github.com/vitest-dev/vitest / ensureAwaited

Function ensureAwaited

packages/browser/src/client/utils.ts:32–66  ·  view source on GitHub ↗
(promise: (error?: Error) => Promise<T>)

Source from the content-addressed store, hash-verified

30}
31
32export function ensureAwaited<T>(promise: (error?: Error) => Promise<T>): Promise<T> {
33 const test = getWorkerState().current
34 if (!test || test.type !== 'test') {
35 return promise()
36 }
37 let awaited = false
38 const sourceError = new Error('STACK_TRACE_ERROR')
39 test.onFinished ??= []
40 test.onFinished.push(() => {
41 if (!awaited) {
42 const error = new Error(
43 `The call was not awaited. This method is asynchronous and must be awaited; otherwise, the call will not start to avoid unhandled rejections.`,
44 )
45 error.stack = sourceError.stack?.replace(sourceError.message, error.message)
46 throw error
47 }
48 })
49 // don't even start the promise if it's not awaited to not cause any unhanded promise rejections
50 let promiseResult: Promise<T> | undefined
51 return {
52 then(onFulfilled, onRejected) {
53 awaited = true
54 return (promiseResult ||= promise(sourceError)).then(onFulfilled, onRejected)
55 },
56 catch(onRejected) {
57 awaited = true
58 return (promiseResult ||= promise(sourceError)).catch(onRejected)
59 },
60 finally(onFinally) {
61 awaited = true
62 return (promiseResult ||= promise(sourceError)).finally(onFinally)
63 },
64 [Symbol.toStringTag]: 'Promise',
65 } satisfies Promise<T>
66}
67
68export interface BrowserRunnerState {
69 files: string[]

Callers 15

cleanupFunction · 0.90
typeFunction · 0.90
tabFunction · 0.90
keyboardFunction · 0.90
createPreviewUserEventFunction · 0.90
screenshotFunction · 0.90
markFunction · 0.90
wheelFunction · 0.90
uploadFunction · 0.90
markFunction · 0.90
triggerCommandFunction · 0.90
clickMethod · 0.85

Calls 2

promiseFunction · 0.85
getWorkerStateFunction · 0.70

Tested by

no test coverage detected