Skip to content

Commit 814d059

Browse files
authored
test: wait for background work to finish in integration tests (#2851)
1 parent d30bca4 commit 814d059

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/utils/fixture.ts

+18
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ export async function invokeFunction(
396396
process.env[key] = environment[key]
397397
})
398398

399+
let resolveInvocation, rejectInvocation
400+
const invocationPromise = new Promise((resolve, reject) => {
401+
resolveInvocation = resolve
402+
rejectInvocation = reject
403+
})
404+
399405
const response = (await execute({
400406
event: {
401407
headers: headers || {},
@@ -405,8 +411,20 @@ export async function invokeFunction(
405411
},
406412
lambdaFunc: { handler },
407413
timeoutMs: 4_000,
414+
onInvocationEnd: (error) => {
415+
// lambda-local resolve promise return from execute when response is closed
416+
// but we should wait for tracked background work to finish
417+
// before resolving the promise to allow background work to finish
418+
if (error) {
419+
rejectInvocation(error)
420+
} else {
421+
resolveInvocation()
422+
}
423+
},
408424
})) as LambdaResponse
409425

426+
await invocationPromise
427+
410428
const responseHeaders = Object.entries(response.multiValueHeaders || {}).reduce(
411429
(prev, [key, value]) => ({
412430
...prev,

0 commit comments

Comments
 (0)