0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 02:49:32 +01:00
posthog/plugin-server/tests/helpers/expectations.ts
2024-09-10 09:05:30 +02:00

18 lines
595 B
TypeScript

export const waitForExpect = async <T>(fn: () => T | Promise<T>, timeout = 10_000, interval = 1_000): Promise<T> => {
// Allows for running expectations that are expected to pass eventually.
// This is useful for, e.g. waiting for events to have been ingested into
// the database.
const start = Date.now()
while (true) {
try {
return await fn()
} catch (error) {
if (Date.now() - start > timeout) {
throw error
}
await new Promise((resolve) => setTimeout(resolve, interval))
}
}
}